14
1.
package zyzy; public class Vehicle { private String brand; private String color; private double speed; public Vehicle(String brand, String color, double speed) { super(); this.brand = brand; this.color = color; this.speed = 0; } 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 zyzy; public class Car extends Vehicle{ int loader; public int getLoader() { return loader; } public void setLoader(int loader) { this.loader = loader; } public Car(String brand, String color, double speed, int loader) { super(brand, color, speed); this.loader = loader; } public void run(){ System.out.println("品牌是"+super.getBrand()+",颜色是"+super.getColor()+",载人数为"+loader+"的汽车,正在以"+super.getSpeed()+"速度奔跑"); } }
package zyzy; public class test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Vehicle v=new Vehicle("benz", "black", 180); v.setSpeed(120); v.run(); Car c=new Car("Honda", "red", 180, 3); c.setSpeed(180); c.run(); } }
2.
package zyzy2; 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 zyzy2; 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 zyzy2; public class test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Rectangle r = new Rectangle(2, 2, '黑'); r.showAll(); Circle c = new Circle(6, '白'); c.showAll(); } }
package zyzy2; 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; } }