1
.(1).
package llll; public class test { public static void main(String[] args) { // TODO 自动生成的方法存根 vehicle v=new vehicle(); v.brand="benz"; v.color="black"; System.out.println(v.brand+" "+v.color); v.run(); } }
package llll; public class vehicle { String brand,color; double speed; public void set(String color,double speed) { this.color=color; this.speed=0; } public String getBrand() { return brand; } public void run() { System.out.println("在高速上行驶"); } }
(2).
package qq; public class text { public static void main(String[] args) { // TODO 自动生成的方法存根 Car1 c=new Car1(); c.brand="Honda"; c.color="red"; c.loader=2; System.out.println("品牌为"+c.brand+"颜色为"+c.color+"载入数为"+c.loader); c.run(); } }
package qq; public class car1 { int loader; public void getCar1() { this.loader=5; } public void run() { System.out.println("轿车在街上行驶"); } }
2.
package qq; 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 qq; 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 qq; 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 qq; public class polydemo { public static void main(String[] args) { // TODO Auto-generated method stub shape a = new Circle(5, "红色"); shape b = new rectangle(1, 4, "绿色"); a.getArea(); a.getPer(); a.showAll(); b.getArea(); b.getPer(); b.showAll(); } }