14

package Qc;

 
public class Vehicle {
    protected String brand;
    protected String color;
    protected double speed;
 
    public Vehicle(String brand, String color, double speed) {
        super();
        this.brand = brand;
        this.color = color;
        this.speed = 0;
    }
 
    public Vehicle() {
        super();
    }
 
    public String getBrand() {
        return brand;
    }
 
    public String getColor() {
        return color;
    }
 
    public void setColor(String color) {
        this.color = color;
    }
 
    public double getSpeed() {
        return speed;
    }
 
    public void setSpeed(double speed) {
        this.speed = speed;
    }
 
    public void run() {
        System.out.println(brand + "品牌" + color + "颜色的汽车" "正在以" + speed
                "的速度奔跑");
    }
}
package Qc;
 
public class T1 {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Vehicle v1 = new Vehicle("benz""black"200);
        v1.run();
 
    }
 
}

 

 package Qc;

 
public class Car extends Vehicle {
    int lader;
 
    public Car(String brand, String color, double speed, int lader) {
        super(brand, color, speed);
        this.lader = lader;
    }
 
    public Car(String brand, String color, double speed) {
        super(brand, color, speed);
    }
 
    public int getLader() {
        return lader;
    }
 
    public void setLader(int lader) {
        this.lader = lader;
    }
 
    public void run() {
        System.out.println(brand + "品牌" + color + "颜色" "载人数为" + lader + "的轿车"
                "正在以" + speed + "的速度奔跑");
    }
}
package Qc;
 
public class T2 {
 
    public static void main(String[] args) {
        Car c = new Car("Honda""red"2002);
        c.run();
    }
}

 

3.

package www;

public abstract class Shape {
    double area;
    double per;
    char color;
    public Shape() {
        super();
    }
    public Shape(char color) {
        super();
        this.color = color;
    }
    public abstract double getPer();
    public abstract double getArea();
    public  abstract void showAll();
    public char getColor() {
        return color;
    }
    
}package www;

public class Rectangle extends Shape {
    @Override
    public double getPer() {
        // TODO Auto-generated method stub
        return (width + height) * 2;
    }

    @Override
    public double getArea() {
        // TODO Auto-generated method stub
        return width * height;
    }

    double width;
    double height;

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

    public Rectangle() {
        super();
    }
    public void showAll() {
        System.out.println("矩形的长是" + width + ",宽是" + height + ",颜色是" + color + ",周长是" + getPer() + ",面积是" + getArea());
    }
}
package www;
public class Circle extends Shape{
    double radius;

    public void showAll(){
        System.out.println("圆形的半径是"+radius+",颜色是"+color+",周长是"+getPer()+",面积是"+getArea());
    }
    @Override
    public double getPer() {
        // TODO Auto-generated method stub
        return 2*3.14*radius;
    }
    @Override
    public double getArea() {
        // TODO Auto-generated method stub
        return 3.14*radius*radius;
    }
    public Circle(double radius,char color) {
        super();
        this.radius = radius;
        this.color=color;
    }
    
}package www;

public class testshape {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根

        Rectangle r = new Rectangle(2, 3, '黑');
        r.showAll();
        Circle c = new Circle(4, '红');
        c.showAll();

    }

}

 

 

 

 

 

posted @ 2021-06-06 17:13  刘德璞  阅读(135)  评论(0编辑  收藏  举报