掌握声明和使用继承类对象的方法
- 格式:doc
- 大小:49.50 KB
- 文档页数:7
【实验目的】1、理解类继承的概念;2、掌握从现有类中继承类;3、掌握声明和使用继承类对象的方法;【实验内容】1、根据下面程序分析类继承的实现过程,给出程序运行之后的结果。
Question5_1.java代码如下:package experiment5;public class Question5_1 {public static void main(String[] args) {B sub = new B();System.out.println("i of B is: " + sub.i);System.out.println("i of B's super is: " + sub.geti());sub.addi();System.out.println("i of B is: " + sub.i);System.out.println("i of B's super is: " + sub.geti());}}class A {int i=100;public void addi() {i++;}}class B extends A {int i=10;public void minusi() {i--;}public int geti() {return super.i;}}结果:如图(1)图(1)2、根据下面给出的客户程序,设计类Shape, Rectangle和Circle。
package experiment5;public class Question5_2 {public static void main(String[] args) {Shape r, c;r = new Rectangle(0.6d, 1.2d);c = new Circle(3.5d);double recArea = r.area();double cirArea = c.area();System.out.println("高为1.2,宽为0.6的矩形面积为:" +recArea);System.out.println("半径为3.5的圆的面积为:" + cirArea);}}代码:public class Shape {double s;double area(){return s;}}public class Rectangle extends Shape {double h;double l;public Rectangle(double h1,double l1){h = h1;l = l1;}double area(){return h*l;}}public class Circle extends Shape {double r;public Circle(double r1){r = r1;}double area(){return (r*r*3.141592654)/2;}}结果:如图(2)图(2)3. 下面是3个类的构造方法,要利用继承改写代码,上机验证你的代码。
package experiment5;class A {String name, color;int age;public A() {name = "Animal";color = "Black";age = 0;}}class B {String name, color;int age;boolean hasLung;public B() {name = "Fish";color = "Golden";age = 1;hasLung = false;}}class C {String name, color;int age;boolean hasTail;public C() {name = "Cat";color = "White";age = 2;hasTail = true;}}代码:public class ABC {String name, color;int age;public ABC(String n,String c,int a){name = n;color = c;age = a;}void show(){System.out.println("name = "+name+" color="+color+" age="+age);}}public class A extends ABC {public A() {super("Animal","Black",0);}void show(){System.out.println("A:");super.show();}}public class B extends ABC {boolean hasLung;public B() {super("Fish","Golden",1);hasLung = false;}void show(){System.out.println("B:");super.show();System.out.println("hasLung="+hasLung);}}public class C extends ABC {boolean hasTail;public C() {super("Cat","White",2);hasTail = true;}void show(){System.out.println("C:");super.show();System.out.println("hasTail="+hasTail);}}public class Testabc {public static void main(String[] args) {A a = new A();B b = new B();C c = new C();a.show();b.show();c.show();}}结果:如图(3)图(3)4、编写Java程序,要求利用方法重载,求任意2个数或3个数的最大数,其中,这2个数或3个数要么全是整数,要么全是浮点数。
提示:(1)方法重载在2个数中使用2次,3个数中使用2次;(2)求3个数的时候也可以使用2个数的方法。
代码:import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class Load {static int max(int a,int b){if (a>b)return a;elsereturn b;}static int max(int a,int b,int c){int max1,max2;if(a>b)max1 = a;elsemax1 = b;if(max1>c)max2 = max1;elsemax2 = c;return max2;}public static void main(String[] args) throws IOException {// TODO Auto-generated method stubBufferedReader input=new BufferedReader(new InputStreamReader(System.in));int a ,b ,c ,max1,max2;System.out.println("请输入您要比较的数:");String chiose1 = input.readLine();a = Integer.parseInt(chiose1);String chiose2 = input.readLine();b = Integer.parseInt(chiose2);max1 = max(a,b);System.out.println("两个数中的最大数:"+max1);System.out.print("是否要继续比较三个数的大小<1-yes/2-no>?:");String choice = input.readLine();int aa = Integer.parseInt(choice);if(aa == 1){System.out.println("请输入第三个数:");String chiose3 = input.readLine();c = Integer.parseInt(chiose3);max2 = max(a,b,c);System.out.println("三个数中的最大数:"+max2);}System.out.println("谢谢您的选择!");}}结果:如图(4)图(4)。