码迷,mamicode.com
首页 > 其他好文 > 详细

第十一周上机练习

时间:2020-05-14 15:22:03      阅读:59      评论:0      收藏:0      [点我收藏+]

标签:owa   xtend   public   loader   speed   ring   demo   div   access   

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();
    }

}

 

第十一周上机练习

标签:owa   xtend   public   loader   speed   ring   demo   div   access   

原文地址:https://www.cnblogs.com/z118127/p/12888590.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!