JAVA例8.9-例8.15及实验35-38.doc_第1页
JAVA例8.9-例8.15及实验35-38.doc_第2页
JAVA例8.9-例8.15及实验35-38.doc_第3页
JAVA例8.9-例8.15及实验35-38.doc_第4页
JAVA例8.9-例8.15及实验35-38.doc_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

例8.9运算符instanceof及getName()、getSuperclass()方法的使用。/filename Person.javapublic class Person /定义person类static int count=0;protected String name;protected int age;public Person(String n1,int a1)name=n1;age=a1;this.count+; /调用父态的静态变量public String toString()return +,+this.age;public void display()System.out.print(本类名=+this.getClass().getName()+;);System.out.println( 父类名=+this.getClass().getSuperclass().getName();System.out.print(Person.count=+this.count+ );System.out.println( Student.count=+Student.count+ );Object obj=this;if(obj instanceof Student) /判断对象属于哪个类System.out.println(obj.toString()+是Student类对象。);else if(obj instanceof Person)System.out.println(obj.toString()+是Person类对象。);class Student extends Person /子类Student继承父类Person,且是主类但不是public类static int count=0; /隐藏了父类的countprotected String dept;protected Student(String n1,int a1,String d1)super(n1,a1); /调用父类的构造方法dept=d1;this.count+; /调用子类的静态变量public String toString() /覆盖父类的同名方法 return super.toString()+,+dept; /调用父类的同名方法public void display()super.display();System.out.println(super.count=+super.count);System.out.println(this.count=+this.count);public static void main(String args)Person per=new Person(王涛,23);per.display();Student stu=new Student(张三,22,计算机系);stu.display();程序运行结果及其分析:例8.10 抽象类的应用举例,定义一个形状抽象类Shape,以该抽象类为父类派生出圆形子类Circle和矩形子类Rectangle。/filename App8_10.javaabstract class Shape /定义形状抽象类Shapeprotected String name;public Shape(String xm) /抽象类中的一般方法,本方法是构造方法name=xm;System.out.print(名称:+name);abstract public double getArea(); /声明抽象方法abstract public double getlength(); /声明抽象方法class Circle extends Shape /定义继承自Shape的圆形子类Circleprivate final double PI=3.14;private double radius;public Circle(String shapeName,double r) /构造方法super(shapeName);radius=r;public double getArea() /实现抽象类中的getArea()方法return PI*radius*radius;public double getlength() /实现抽象类中的getlength()方法return 2*PI*radius;class Rectangle extends Shapeprivate double width;private double height;public Rectangle(String shapeName,double width,double height)super(shapeName);this.width=width;this.height=height;public double getArea()return width*height;public double getlength()return 2*(width+height);public class App8_10public static void main(String args)Shape rect=new Rectangle(长方形,6.5,10.3);System.out.print(; 面积=+rect.getArea()+ ;周长=+rect.getlength()+n);Shape circle=new Circle(圆,10.2);System.out.print(; 面积=+circle.getArea()+ ;周长=+circle.getlength();程序运行结果及其分析:例8.11 利用形状接口IShape建造类。/filename App8_11.javainterface IShapefinal double PI=3.14;abstract double getArea();abstract double getlength();class Circle implements IShapedouble radius;public Circle(double r)radius=r;public double getArea()return PI*radius*radius;public double getlength()return 2*PI*radius;class Rectangle implements IShapeprivate double width;private double height;public Rectangle(double width,double height)this.width=width;this.height=height;public double getArea()return width*height;public double getlength()return 2*(width+height);public class App8_11public static void main(String args)IShape circle=new Circle(5.0);System.out.print(圆的面积=+circle.getArea()+ ; 周长=+circle.getlength()+n);Rectangle rect=new Rectangle(6.5,10.8);System.out.print(矩形面积=+rect.getArea()+ ; 周长=+rect.getlength();例8.12 接口的继承。/filename Cylinder.javainterface Face1final double PI=3.14;abstract double area();interface Face2abstract void setColor(String c);interface Face3 extends Face1,Face2abstract void volume();public class Cylinder implements Face3private double radius;private int height;protected String Color;public Cylinder(double r,int h)radius=r;height=h;public double area()return PI*radius*radius;public void setColor(String c)Color=c;System.out.println(颜色:+Color);public void volume()System.out.println(圆柱体体积:+area()*height);public static void main(String args)Cylinder volu=new Cylinder(3.0,2);volu.setColor(红色);volu.volume();例8.13 利用接口实现类的多重继承。/filename Cylinder.javainterface Face1final double PI=3.14;abstract double area();interface Face2abstract void volume();public class Cylinder implements Face1,Face2private double radius;private int height;public Cylinder(double r,int h)radius=r;height=h;public double area()return PI*radius*radius;public void volume()System.out.println(圆柱体体积:+area()*height);public static void main(String args)Cylinder volu=new Cylinder(5.0,2);volu.volume();例8.14内部与外部的访问规则。/filename Group.javapublic class Groupprivate int age;public class StudentString name;public Student(String n,int a)name=n;age=a;public void output()System.out.println(姓名:++; 年龄:+age);public void output()Student stu=new Student(刘洋,24);stu.output();public static void main(String args)Group g=new Group();g.output();实验35抽象类/filename ChouXiang.javaabstract class Shapeabstract public float Area();abstract public void printArea();class Rectangle extends Shapeint width;int length;public Rectangle(int newWidth,int newLength)width=newWidth;length=newLength;public float Area()return width*length;public void printArea()System.out.println(矩形的面积是+Area();class Circle extends Shapefinal float pi=3.14F;int radius;public Circle(int newRadius)radius=newRadius;public float Area()return pi*radius*radius;public void printArea()System.out.println(圆的面积是+Area();class ChouXiangpublic static void main(String args)Rectangle s1=new Rectangle(3,4);Circle s2=new Circle(2);s1.printArea();s2.printArea();实验37 使用JDK参考文档编写java程序。/UseJDK.javaimport java.lang.Math;public class UseJDKpublic static void main(String args)int a;a=(int)(Math.random()*10);System.out.println(a=+a+的平方根为+Math.sqrt(a);实验38 为java程序生成文档。/GdufStudent.java/*这个程序定义了一个学生类,类中有三个属性,七个方法,可以调用introduce()方法,显示学生的姓名、年龄和系别。*/public class GdufStudent/*公共属性,字符串型,学校的名字*/public String School;private String name;private int age;privat

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论