Java第11次作业

1.

package sdf;

public class Vehicle {

    private String brand;  
    private String color;
    private double speed;
    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 Vehicle(String brand,String color){
        this.brand=brand;
        this.color=color;
        this.speed=0;
    }
    void ran(){
        System.out.println("品牌:"+brand+"   颜色:"+color+"   速度:"+speed);
    }
}
package sdf;

public class Car extends Vehicle{

    private int loader;
    
    public Car(String brand,String color,int loader){
        super(brand,color);
        this.loader=loader;
    }

    void ran(){
        System.out.println("品牌:"+getBrand()+"  颜色:"+getColor()+"   速度:"+getSpeed()+"  载人:"+loader);
    }
}
package sdf;

public class test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Vehicle ve=new Vehicle("benchi","blake");
        ve.setSpeed(123.7);
        ve.ran();
        Vehicle c=new Car("baoma","red",2);
        c.ran();
    }

}

2.

package sdf;

public abstract class Shape {

    private double area;
    private double per;
    private String color;
    public Shape(){
        
    }
    public Shape(String color) {
        this.color=color;
    }
    abstract double getArea();
    abstract double getPer();
    abstract void showAll();
    String getColor(){
        return this.color;
    }
    
}
package sdf;

public class Rectangle extends Shape{

    int height;
    int width;
    public Rectangle(){
        
    }
    public Rectangle(int width,int height,String color){
        super(color);
        this.height=height;
        this.width=width;
    }
    double getPer(){
        return 2*(width+height);
    }
    double getArea(){
        return width*height;
    }
    void showAll(){
        System.out.println("宽度:"+width+"  高度: "+height+" 颜色: "+getColor()+" 周长: "+getPer()+" 面积: "+getArea());
    }
}
package sdf;

public class Circle extends Shape{

    private int radius;
    public Circle(){
        
    }
    public Circle(int radius,String color){
        super(color);
        this.radius=radius;
    }
    double getArea(){
        return 3.14*radius*radius;
    }
    double getPer(){
        return 2*3.14*radius;
    }
    void showAll(){
        System.out.println("半径是: "+radius+" 周长是: "+getPer()+"面积是:"+getArea()+" 颜色: "+getColor());
    }
}

package sdf;

public class test2 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Rectangle s1=new Rectangle(12,2,"red");
        s1.showAll();
        Circle s2=new Circle(3,"ww");
        s2.showAll();
    }  
 }

posted @ 2021-06-07 20:38  jth12  阅读(40)  评论(0编辑  收藏  举报