《Java程序设计案例教程(微课版)》 实验十二封装与继承.docx
实验十二封装与继承一、实验目的:L掌握类的继承的用法.掌握类的继承中this、super关键字的用法二、实验内容:2 .分析程序的结果,并上机验证。class Saint x,y;public void setXy(int x,int y) this.x=x;this.y=y;)public int result() return x+y; )class Sb extends Saint x;public int result()super. x=5;x=super.result()/x;return x;class Sc public static void main(String aa) Sa s=new Sa();s.setXy(5,10);System.out.println(nsa result:n+s.result();Sb s2=new Sb();s2.x=3;s2.y=10;System.out.println(Hsb result:n+s2.result();)输出结果为:sa result: 15sb result:53 .阅读程序,分析程序的输出结果。class A protected void print(String s) System.out.printin(s);A() print (ffA() ; public void f() print("A:f()");) class B extends A B() print (nB() 11 ) ; public void f () print (nB : f () n) ; public static void main(String arg) B b = new B () ; b . f ();输出结果为:A:f()B:f()B:f().定义一个矩形类,它包含两个数据成员:长和宽,以及包含用于求面积的成员方法。再 定义矩形类的子类一长方体类,增加一个数据成员高以及用来求长方体体积的成员方法。 在主方法中用其求长方形的面积和长方体的体积。分析:这个题目我们在前面学习类与对象时,采用了面向对象的思想这样实现的:第一步:封装长方形的类plass Rect_/double lzw;/ Rect(double 1,double w) /this.1=1;this.w-w;)double getL() return 1;)void setL (double 1) this.1=1;double getArea() return 1 w;)第二步:封装长方体类弋lass Cuboid 长方体类:|P| (长方形)+h ?<ect r;double h;Cuboid(Rect r,double h) this . r=r;this.h=h;)double getV() 体积=面积*hreturn r.getArea() h;)第三步:测试类 public class TestRect public static void main (String args) Rect r new Rect (3, 4) ;/先而造长方形 Cuboid c = new Cuboid (r, 8);在上而长方形的基础上构造长方体 System.out.printin ("面积是:n+c.getV ();)请用刚学习的继承来实现长方体类的定义。源程序:public class 长方体类 (public static void main(String args) / TODO自动生成的方法存根changfangti a=new changfangti (3,4,5);System.out.printin(a.mianj i(); System.out.printin(a.tij i ();)class juxingl double a,b; juxingl(double a,double b) this . a=a;this .b=b;)double mianj i() return a*b;)class changfangti extends juxingl double gao;double mianj i;changfangti(double a,double b,double gao) super (a,b);this .gao=gao;)double tij i () return mianj i()*gao;)4.借用继承,根据下面的类图优化电子宠物系统。源程序:class petprivate String name;pet (String name) this . name=name;)public String getName() return name;)public void setName(String name) this.name = name;public void eat () System, out .printin (this . name+”开始干饭”);)public void j ieshao () System. out .printIn ("亲爱的铲屎官,我的名字叫 n+this . name);)class Cat extends petprivate String pinzhong;Cat (String name,String pinzhong) super(name); this.pinzhong=pinzhong;public String getPinzhong() return pinzhong;)public void setPinzhong (String pinzhong) this.pinzhong = pinzhong;)public void j ieshao() super . j ieshao ();System. out .printIn ("亲爱的铲屎官,我的名字叫 n + this . getName () +”我是只纯种的”+this . pinzhong);)public void wanmaoxiantuan() System, out .printin (this . gtNam()+”正在玩毛线团 ”);)class Dog extends pet private String sex; Dog(String name,String sex) super(name);this . sex=sex;)public String getSex() return sex;)public void setSex (String sex) this.sex = sex;public void jiehsao() super . j ieshao ();System, out .printin (“我是一只”+this . sex); public void diaofeipan() System, out .printin (this . getName () +”正在玩飞盘 ”); ) ) class Penguin extends pet private String sex;Penguin(String name,String sex) super(name); this . sex=sex;public String getSex() return sx;)public void setSex (String sex) this.sex = sex;public void jiehsao() super . j ieshao (); System, out .printin (“我是一只”+this . sex);public void diaofeipan() Systm. out .printin (this . getName () +”正在滑冰");)三.实验要求:1、事先预习,写出预习报告2、上机后写出实验报告