第十一周上机练习

1.

package test;
public class Vehicle {
     public String brand;
        public String color;
        public double speed=0;
        void setVehicle(String brand,String color) {
            this.brand=brand;
            this.color=color;
        }
        void access(String brand,String color,double speed) {
            this.brand=brand;
            this.color=color;
            this.speed=speed;
        }
        void run() {
        System.out.println("车的品牌是"+this.brand+" 颜色是"+this.color+" 速度是"+this.speed);
        }
}
package test;
public class VehicleTest {
     public static void main(String[] args) {
         Vehicle m=new Vehicle();
            m.access("a" ,"black", 450);
            m.run();
     }
}
package test;
public class Car extends Vehicle {
     int loader;
        void access(String brand,String color,double speed,int loader) {
            this.brand=brand;
            this.color=color;
            this.speed=speed;
            this.loader=loader;
        }
        void run() {
            System.out.println("车的品牌是"+this.brand+" 颜色是"+this.color+" 速度是"+this.speed+" 载人数是"+this.loader);
        }
}
package test;
public class LH {
    public static void main(String[] args) {
         Car n=new Car();
            n.access("b", "red", 330, 4);
            n.run();
    }
}

2.

package test;
public abstract class Shape {
    protected double area;
    protected double per;
    protected String color;

    public Shape() {
    }

    public Shape(String color) {
        this.color = color;
    }

    public abstract void getArea();

    public abstract void getPer();

    public abstract void showAll();

}
package test;
public class Rectangle extends Shape {
    double width;
    double height;

    public Rectangle() {
    }

    public Rectangle(double width, double height, String color) {
        super();
        this.width = width;
        this.height = height;
        this.color = color;
    }

    public void getArea() {
        area = width * height;
    }

    public void getPer() {
        per = (width + height) * 2;
    }

    public void showAll() {
        System.out.println("矩形面积为:" + area + ",周长为:" + per + ",颜色为:" + color);
    }
}
package test;
public class Circle extends Shape {
    double radius;

    public Circle() {
    }

    public Circle(double radius, String color) {
        this.color = color;
        this.radius = radius;
    }

    public void getArea() {
        area = radius * radius * 3.14;
    }

    public void getPer() {
        per = 2 * radius * 3.14;
    }

    public void showAll() {
        System.out.println("圆的面积为:" + area + ",周长为:" + per + ",颜色为:" + color);
    }
}
package test;
public class PolyDemo {

    public static void main(String[] args) {
        Shape a = new Circle(4, "蓝色");
        Shape b = new Rectangle(6, 8, "黑色");
        a.getArea();
        a.getPer();
        a.showAll();
        b.getArea();
        b.getPer();
        b.showAll();
    }

}

 

posted @ 2020-05-14 14:47  计算机1804梁浩  阅读(144)  评论(0编辑  收藏  举报