第十一次上机作业
package Wxte; public class Car { private String brand; private String color; private double speed = 0; public Car() { } public Car(String brand, String color) { this.brand = brand; this.color = color; speed = 0; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = 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(getColor() + "色的 " + getBrand() + " 的速度是 " + getSpeed()); } }
package Wxte; public class desk { public static void main(String[] args) { // TODO Auto-generated method stub Car car = new Car("desk","blue"); car.run(); } }
package Wxte; public class besk extends Car { private int loader; public besk() { } public besk(String brand, String color, int loader) { super(brand, color); this.loader = loader; } public void run() { System.out.println(getColor() + "色的" + getBrand()+"载人人数是:" + loader + "的速度是:" + getSpeed()); } }
package Wxte; public class Bases { public static void main(String[] args) { // TODO Auto-generated method stub Car besk = new besk("desks", "blow",4 ); besk.run(); } }
2.
package Wxte; public abstract class Car { protected double area; protected double per; protected String color; public Car () { } public Car (String color) { this.color = color; } public abstract void getArea(); public abstract void getPer(); public abstract void showAll(); }
package Wxte; public class besk extends Car { double width; double height; public besk () { } public besk (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 Wxte; public class Dsksk extends Car { double radius; public Dsksk() { } public Dsksk(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 Wxte; public class desk { public static void main(String[] args) { // TODO Auto-generated method stub Car a = new Dsksk(3, "蓝色"); Car b = new besk(5, 7, "白色"); a.getArea(); a.getPer(); a.showAll(); b.getArea(); b.getPer(); b.showAll(); } }