javaSE各阶段练习题--面向对象-多态-抽象类-接口
生活随笔
收集整理的這篇文章主要介紹了
javaSE各阶段练习题--面向对象-多态-抽象类-接口
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1 (多態(tài))定義榨汁機JuiceMachine 有榨汁方法makeJuice,傳入相應(yīng)的水果。如果傳入的是Apple 輸出 "流出蘋果汁"傳入的是Orange 輸出 "流出橙汁"傳入的是Banana 輸出 "流出香蕉醬"
public class Test01 {public static void main(String[] args) {JuiceMachine jMachine = new JuiceMachine();Apple apple = new Apple();jMachine.makeJuice(apple);//流出蘋果汁Orange orange = new Orange();jMachine.makeJuice(orange);Banana banana = new Banana();jMachine.makeJuice(banana);}}class JuiceMachine{public void makeJuice(Fruit f) {f.make();}
}class Fruit{public void make() {System.out.println("制作水果汁");}
}class Apple extends Fruit{public void make() {System.out.println("流出蘋果汁");}
}class Orange extends Fruit{public void make() {System.out.println("流出橙汁");}
}class Banana extends Fruit{public void make() {System.out.println("流出香蕉醬");}
}2 (多態(tài))已知形狀類 擁有一個計算面積的方法子類 矩形、圓形、三角形 覆蓋形狀類的計算面積的方法 分別計算矩形、圓形、三角形的面積 已知一個Object類型數(shù)組 裝有 五個(各個圖形的個數(shù)不定) 矩形、圓形、三角形的對象計算這五個對象的面積總和設(shè)計形狀類 和它的三個子類,完成上面的工作,
提示: 三角形面積等于 半周長*(半周長-邊1)*(半周長-邊2)*(半周長-邊2) 的積 開根號(海倫定理)如何開根號,請自行查閱Math類public class Test02 {public static void main(String[] args) {Form rectangle = new Rectangle(5,4);Form rectangle2 = new Rectangle(8, 9);Form circle = new Circle(4);Form circle2 = new Circle(5);Form triangle = new Triangle(3, 4, 5);Object[] os = new Object[5];os[0] = rectangle;os[1] = rectangle2;os[2] = circle;os[3] = circle2;os[4] = triangle;double sum = 0;for (int i = 0; i < os.length; i++) {sum += ((Form) os[i]).cacluateArea();}System.out.println("面積總和為="+sum);}
}//形狀類
class Form{public double cacluateArea() {return 0;}
}//矩形類
class Rectangle extends Form{public Rectangle(int length, int width) {super();this.length = length;this.width = width;}private int length;//長private int width;//寬public double cacluateArea() {return length*width;}
}
//圓
class Circle extends Form{public Circle(int r) {super();this.r = r;}private double r;//半徑public double cacluateArea() {return (3.14*r*r);}
}
//三角
class Triangle extends Form{private int L1;//三邊長度private int L2;private int L3;public Triangle(int l1, int l2, int l3) {super();L1 = l1;L2 = l2;L3 = l3;}//三角形面積等于 半周長*(半周長-邊1)*(半周長-邊2)*(半周長-邊2) 的積 開根號public double cacluateArea() {int s = (L1+L2+L3)/2;int res = s*(s-L1)*(s-L2)*(s-L2);return ((int)(Math.sqrt(res)*100))/100.0;}
}3 (抽象類) 雇員類(Employee-抽象類):包含抽象方法work() 和抽象方法 show()work()方法表示 工作內(nèi)容show()方法表示 員工屬性的介紹程序員類:屬性(姓名、工號、工資、獎金),行為(工作:軟件開發(fā))測試工程師:屬性(姓名、工號、工資),行為(工作:軟件測試)項目經(jīng)理類:屬性(姓名、工號、工資、獎金),行為(工作:控制進度)要求:子類在實現(xiàn)時,用System.out.println()在控制臺輸出例如: 程序員 work() 輸出:"軟件開發(fā)"show() 輸出:姓名為xxx 工號為xxx ......public class Test03 {public static void main(String[] args) {Employee e1 = new Programmer("技術(shù)總監(jiān)", 30003, 16000, 4000);e1.work();e1.show();Employee e2 = new TestEngineer("首席測試師", 20001, 13000);e2.work();e2.show();Employee e3 = new Programmer("李孟冬", 10001, 20000, 8000);e3.work();e3.show(); }
}abstract class Employee{//雇員類private String name;//姓名private int id;//工號private int salary;//工資public Employee(String name, int id, int salary) {super();this.name = name;this.id = id;this.salary = salary;}public String getName() {return name;}public int getId() {return id;}public int getSalary() {return salary;}public abstract void work();//工作內(nèi)容public abstract void show();//員工屬性的介紹
}
//程序猿類
class Programmer extends Employee{private int prize;//獎金public Programmer(String name, int id, int salary, int prize) {super(name, id, salary);this.prize = prize;}@Overridepublic void work() {System.out.println("我的工作是軟件開發(fā)。");}@Overridepublic void show() {System.out.println("姓名:"+super.getName()+",工號:"+super.getId()+",工資:"+super.getSalary()+",獎金:"+this.prize);}
}
//測試工程師
class TestEngineer extends Employee{public TestEngineer(String name, int id, int salary) {super(name, id, salary);}@Overridepublic void work() {System.out.println("我的工作是軟件測試。");}@Overridepublic void show() {System.out.println("姓名:"+super.getName()+",工號:"+super.getId()+",工資:"+super.getSalary());}
}
//項目經(jīng)理
class ProjectManager extends Employee{private int prize;//獎金public ProjectManager(String name, int id, int salary, int prize) {super(name, id, salary);this.prize = prize;}@Overridepublic void work() {System.out.println("我的工作是控制進度。");}@Overridepublic void show() {System.out.println("姓名:"+super.getName()+",工號:"+super.getId()+",工資:"+super.getSalary()+",獎金:"+this.prize);}
}4(接口)定義一個接口Area,其中包含一個計算面積的抽象方法calculateArea(),
然后設(shè)計MyCircle和MyRectangle兩個類都實現(xiàn)這個接口中的方法calculateArea(),
分別計算圓和矩形的面積。
public class Test04 {public static void main(String[] args) {MyCircle circle = new MyCircle(2);circle.calculateArea();MyRactangle ractangle = new MyRactangle(5, 8);ractangle.calculateArea();}
}interface Area{void calculateArea();
}class MyCircle implements Area{private double r;public MyCircle(double r) {this.r = r;}@Overridepublic void calculateArea() {System.out.println(3.14*r*r);}
}
class MyRactangle implements Area{private int length;private int width;public MyRactangle(int length, int width) {this.length = length;this.width = width;}@Overridepublic void calculateArea() {System.out.println(length*width);}
}5 .編寫一個Shape接口,
具有一個draw 方法,
并編寫三個類Triangle,Rectangle,Diamond都實現(xiàn)Shape 接口。
在3個類中分別實現(xiàn)draw方法打印如下星陣:* **** **** **** ***
***** **** *
編寫一個測試類具有一個測試方法,
使用Shape 參數(shù),
方法體中調(diào)用Shape的draw 方法,打印出相應(yīng)圖形
public class Test05 {public static void main(String[] args) {Tria t = new Tria();t.draw();System.out.println("-------------");Rect r = new Rect();r.draw();System.out.println("-------------");Diamond d = new Diamond();d.draw();}
}interface Shape{void draw();
}class Tria implements Shape{@Overridepublic void draw() {for (int i = 1; i <= 3; i++) {for (int j = i; j <= 2; j++) {System.out.print(" ");}for (int j = 1; j <= 2*i-1; j++) {System.out.print("*");}System.out.println();}}
}
class Rect implements Shape{@Overridepublic void draw() {for(int i = 0; i < 3; i++) {for(int j = 0; j < 4; j++) {System.out.print("*");}System.out.println();}}
}
class Diamond implements Shape{@Overridepublic void draw() {System.out.println(" * ");System.out.println("***");System.out.println(" * ");}
}
?
總結(jié)
以上是生活随笔為你收集整理的javaSE各阶段练习题--面向对象-多态-抽象类-接口的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: javaSE各阶段练习题--面向对象-S
- 下一篇: javaSE各阶段练习题--工具类-常用