1 package com.wsy.work; 2 3 public class Monkey { 4 public Monkey(String s) 5 { 6 7 } 8 public void speak() 9 { 10 System.out.println("咿咿呀呀......"); 11 } 12 }
1 package com.wsy.work; 2 3 public class People extends Monkey{ 4 public People(String s) 5 { 6 super(s); 7 } 8 @Override 9 public void speak() 10 { 11 System.out.println("小样的,不错嘛!会说话了!"); 12 } 13 public void think() 14 { 15 System.out.println("别说话!认真思考!"); 16 } 17 }
1 package com.wsy.work; 2 3 public class E { 4 public static void main(String[] args) { 5 Monkey m1 = new Monkey("m1"); 6 People p1 = new People("p1"); 7 People p2 = new People("p2"); 8 m1.speak(); 9 p1.speak(); 10 p2.speak(); 11 p1.think(); 12 } 13 }
1 package com.wsy.work; 2 3 import java.math.BigDecimal; 4 5 public class Rectangle { 6 private BigDecimal length; 7 private BigDecimal width; 8 public Rectangle(BigDecimal length, BigDecimal width) 9 { 10 this.length = length; 11 this.width = width; 12 } 13 public BigDecimal arithmeticArea() 14 { 15 return this.length.multiply(width); 16 } 17 public BigDecimal getLength() { 18 return length; 19 } 20 public void setLength(BigDecimal length) { 21 this.length = length; 22 } 23 public BigDecimal getWidth() { 24 return width; 25 } 26 public void setWidth(BigDecimal width) { 27 this.width = width; 28 } 29 30 }
1 package com.wsy.work; 2 3 import java.math.BigDecimal; 4 5 public class Cuboid extends Rectangle { 6 private BigDecimal hight; 7 public Cuboid(BigDecimal length, BigDecimal width, BigDecimal hight) 8 { 9 super(length , width); 10 this.hight = hight; 11 } 12 @Override 13 public BigDecimal arithmeticArea() 14 { 15 return this.hight.multiply(getLength()).multiply(getWidth()); 16 } 17 public BigDecimal getHight() { 18 return hight; 19 } 20 public void setHight(BigDecimal hight) { 21 this.hight = hight; 22 } 23 }
1 package com.wsy.work; 2 3 import java.math.BigDecimal; 4 5 public class Test { 6 7 public static void main(String[] args) { 8 Rectangle r1 = new Rectangle(new BigDecimal("5.14"), new BigDecimal("2.12")); 9 Cuboid c1 = new Cuboid(new BigDecimal("8.4") , new BigDecimal("3.2") , new BigDecimal("4.1")); 10 System.out.println(r1.arithmeticArea()); 11 System.out.println(c1.arithmeticArea()); 12 } 13 14 }
1 package com.wsy.work; 2 3 import java.math.BigDecimal; 4 5 public class Vehicle { 6 private int wheels; 7 private BigDecimal weight; 8 public Vehicle(int wheels , BigDecimal weight) 9 { 10 this.wheels = wheels; 11 this.weight = weight; 12 } 13 public int getWheels() { 14 return wheels; 15 } 16 public void setWheels(int wheels) { 17 this.wheels = wheels; 18 } 19 public BigDecimal getWeight() { 20 return weight; 21 } 22 public void setWeight(BigDecimal weight) { 23 this.weight = weight; 24 } 25 @Override 26 public String toString() 27 { 28 return "wheels"+this.wheels+" weight"+this.weight; 29 30 } 31 }
1 package com.wsy.work; 2 3 import java.math.BigDecimal; 4 5 public class Car extends Vehicle{ 6 private int loader; 7 public Car(int wheels , BigDecimal weight , int loader) 8 { 9 super(wheels , weight); 10 this.loader = loader; 11 } 12 public int getLoader() { 13 return loader; 14 } 15 public void setLoader(int loader) { 16 this.loader = loader; 17 } 18 @Override 19 public String toString() 20 { 21 return "wheels"+this.getWheels()+" weight"+this.getWeight()+" loader"+this.loader; 22 } 23 }
1 package com.wsy.work; 2 3 import java.math.BigDecimal; 4 5 public class Truck extends Car{ 6 private BigDecimal payload; 7 public Truck(int wheels , BigDecimal weight , int loader , BigDecimal payload) 8 { 9 super(wheels , weight , loader); 10 this.payload = payload; 11 } 12 public BigDecimal getPayload() { 13 return payload; 14 } 15 public void setPayload(BigDecimal payload) { 16 this.payload = payload; 17 } 18 @Override 19 public String toString() 20 { 21 return "wheels"+this.getWheels()+" weight"+this.getWeight()+" loader"+this.getLoader()+" payload"+this.payload; 22 23 } 24 }
1 package com.wsy.work; 2 3 import java.math.BigDecimal; 4 5 public class Test2 { 6 7 public static void main(String[] args) { 8 Vehicle v1 = new Vehicle(20, new BigDecimal("2.8")); 9 System.out.println(v1); 10 Car c1 = new Car(30, new BigDecimal("3.1"), 7); 11 System.out.println(c1); 12 Truck t1 = new Truck(40, new BigDecimal("5.1"), 8, new BigDecimal("20")); 13 System.out.println(t1); 14 } 15 16 }