ImageVerifierCode 换一换
格式:DOCX , 页数:47 ,大小:47.01KB ,
资源ID:8024000      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/8024000.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(ch05java高级类特性.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

ch05java高级类特性.docx

1、ch05java高级类特性第5章 高级类特性第一节 抽象类与抽象方法一、类的类型类的类型:最终类(final)、抽象类(abstract)、默认类。抽象类(abstract) 一个abstract类不能直接创建对象,必须通过其子类才能创建对象。最终类(final) 一个最终类不能再派生子类。默认类 可以有子类,也可以生成对象。二、抽象(abstract)类1.抽象方法抽象方法是指只给出定义,但不包含实现语句的方法。抽象方法它没有大括号,有大括号但大括号中没有任何内容的方法,仍不是抽象方法。如:public abstract void lx();2.抽象类的概念用abstract修饰的类称为抽象

2、类。一般的抽象类都包括抽象方法.这种类不能创建对象,因为它的完整实现还没有定义。只能由其创建子类(抽象类是专门用来作为其它类的父类的)。3.抽象类的定义在类定义的class关键词前放置关键词abstract即可。格式:abstract class 类名如:public abstract class Student String id; String name; String speciality; public Student(String id, String name,String speciality ) this.id=id; this.name=name; this.speciali

3、ty=speciality; / public Student( )/ this.id=null;/ this.name=null;/ this.speciality=null;/ public void setId(String id) this.id=id; public String getId() return id; public String getName() return name; public void setName(String name) this.name = name; public String getSpeciality() return speciality

4、; public void setSpeciality(String speciality) this.speciality = speciality; public String getStudentInfo() return id=+getId() +name=+getName() +speciality=+getSpeciality(); public abstract int sum();public class AgricultureStudent extends Student private int botany; private int gengzuo; public Agri

5、cultureStudent(String id, String name,String speciality ,int botany,int gengzuo) /*父类没定义构造方法或有无参构造方法 super();/super可省略 / this.id=id;/ this.name=name;/ this.speciality=speciality; /this.botany=botany; */ /父类有参数时 super(id,name,speciality); this.botany=botany; this.gengzuo=gengzuo; public int getBotany

6、() return botany; public void setBotany(int botany) this.botany = botany; Override public String getStudentInfo() return super.getStudentInfo() +score=+getBotany(); Override public int sum() return gengzuo+botany; public class StudentTest public static void main(String args) AgricultureStudent agicu

7、ltureStudent=new AgricultureStudent(123, li,agriculture,90,67); System.out.println(id+agicultureStudent.getId() +sum=+agicultureStudent.sum(); /* * 没有抽象方法的抽象类 * author tai */public abstract class ABDemo int age = 10; void setAge(int a) age = a; public static void main(String a) ABDemo a1 = new ABDem

8、o();/此命令会出错,抽象类不能生成对象 4、实例public class Polymorphism public static void main(String args) Triangle t=new Triangle(5.0,2.0); t.show_area(); Rectangle r=new Rectangle(3.0,4.0); r.show_area(); Circle c=new Circle(10.0); c.show_area(); abstract class Figure protected double x=10.0,y=5.0; abstract void sh

9、ow_area();class Triangle extends Figure Triangle(double a,double b) x=a; y=b; void show_area() System.out.println(triangle:+(0.5*x*y); class Rectangle extends Figure Rectangle(double a,double b) x=a; y=b; void show_area() System.out.println(rectangle:+(x*y); class Circle extends Figure Circle(double

10、 a)x=a; final double pi=3.1416; void show_area() System.out.println(circle:+(pi*x*x); 实例二、public abstract class Math int answer; void show() System.out.print(answer+answer); abstract void add(int a,int b);/计算a+b abstract void sub(int a,int b);/计算a-b abstract void mul(int a,int b);/计算a+b abstract voi

11、d div(int a,int b);/a/bclass Compute extends Math /在此类里继承了show void add(int a, int b) answer = a + b; void sub(int a, int b) answer = a - b; void mul(int a, int b) answer = a * b; void div(int a, int b) answer = a / b; public class App public static void main(String args) Compute cmp=new Compute();

12、cmp.add(3, 5); cmp.show(); 强调: 抽象类的子类必须在类中实现抽象类中的抽象方法,即给出抽象方法的定义,它才不是抽象类。5、强调将类Triangle方法中的show_area()去掉,前必须加abstract关键字,为什么?因为类Triangle没有对show_area()进行重写,而是直接继承了抽象方法public class Polymorphism public static void main(String args) Triangle t=new Triangle(5.0,2.0); t.show_area(); Rectangle r=new Rectan

13、gle(3.0,4.0); r.show_area(); Circle c=new Circle(10.0); c.show_area(); abstract class Figure protected double x=10.0,y=5.0; abstract void show_area();abstract class Triangle extends Figure Triangle(double a,double b) x=a; y=b; class Rectangle extends Figure Rectangle(double a,double b) x=a; y=b; voi

14、d show_area() System.out.println(rectangle:+(x*y); class Circle extends Figure Circle(double a)x=a; final double pi=3.1416; void show_area() System.out.println(circle:+(pi*x*x); 强调:不使用抽象类,如果子类没有覆盖超类中的非抽象方法,也不会出错,但没有意义 抽象类可否有自己的构造方法? 答案是: 抽象类中允许有自己的构造方法,但是该构造方法并不能直接实例化自己的对象. 如果在抽象类中存在有参构造方法,则必须在子类中明确

15、的使用super(参数列表)指明要调用父类中的哪个构造方法.四、为什么要使用抽象类1、抽象类是规范为了使抽象类的子类在完成相同或相似的功能时,具有相同的方法名。(在规化类库时,在抽象类中定义出抽象方法,使实现它的子类的程序员去实现。2、代码重用可以继承.3、多态为了实现多态。第二节 抽象类与多态一、实例一abstract class Figure protected double x=10.0,y=5.0; abstract void show_area();class Triangle extends Figure Triangle(double a,double b) x=a; y=b;

16、void show_area() System.out.println(triangle:+(0.5*x*y); class Rectangle extends Figure Rectangle(double a,double b) x=a; y=b; void show_area() System.out.println(rectangle:+(x*y); class Circle extends Figure Circle(double a)x=a; final double PI=3.1416; void show_area() System.out.println(circle:+(P

17、I*x*x); 测试一:public class Test1 public static void main(String args) / Figure f=new Triangle(5.0,2.0);/ f.show_area();/ f=new Rectangle(3.0,4.0);/ f.show_area();/ f=new Circle(10.0);/ f.show_area(); 测试二:public class Test2 public static void main(String args) Figure f =new Figure3;/为什么 f0=new Triangle

18、(5.0,2.0); f1=new Rectangle(6.0,87.0); f2=new Circle(100.0); for(Figure f1:f) f1.show_area(); 测试三:public class Test3 public static void show(Figure f) f.show_area(); public static void main(String args) Triangle triangle = new Triangle(5.0, 2.0); Rectangle rectangle = new Rectangle(3.0, 4.0); Circle

19、 circle = new Circle(10.0); show(triangle); show(circle); show(rectangle); 测试四:public class Test4 public static void show(Rectangle rec) for(Rectangle rectangle:rec) rectangle.show_area(); public static void main(String args) Rectangle rectangle = new Rectangle2; rectangle0 = new Rectangle(3.0, 4.0)

20、; rectangle1 = new Rectangle(5.0, 4.0); show(rectangle); 二、实例二abstract class Figure protected double x=10.0,y=5.0; abstract double area();class Rectangle extends Figure Rectangle(double a,double b) x=a; y=b; double area() return (x*y); class Triangle extends Figure double z; Triangle(double a,double

21、 b,double c) x=a; y=b; z=c; double area() double p=(x+y+z)/2; return Math.sqrt(p*(p-x)*(p-y)*(p-z); class Circle extends Figure Circle(double a)x=a; double area() return Math.PI*x*x; 测试一:public class Test4 public static double totalArea1(Figure f) double totalArea=0; for(Figure f1:f) totalArea+=f1.a

22、rea(); return totalArea; public static void main(String args) Figure f =new Figure3;/为什么 f0=new Triangle(5.0,6.0,7.0); f1=new Rectangle(6.0,87.0); f2=new Circle(100.0); double totalArea=0; for(Figure f1:f) totalArea+=f1.area(); System.out.println(totalarea=+totalArea); System.out.println(totalarea1=

23、+totalArea1(f); 测试二:import javax.swing.JOptionPane;/* * * author tai */public class Test5 public static double totalArea1(Figure f) double totalArea=0; for(Figure f1:f) totalArea+=f1.area(); return totalArea; public static void main(String args) Figure f =new Figure3;/为什么 f0=new Triangle(5.0,6.0,7.0

24、); f1=new Rectangle(6.0,87.0); String input=JOptionPane.showInputDialog(输入圆的半径); double r=Double.parseDouble(input); f2=new Circle(r); double totalArea=0; for(Figure f1:f) totalArea+=f1.area(); JOptionPane.showMessageDialog(null,totalarea=+totalArea); JOptionPane.showMessageDialog(null,totalarea1=+t

25、otalArea1(f); 测试三:import javax.swing.JOptionPane;/* * * author tai */public class Test6 public static double totalArea1(Figure f) double totalArea=0; for(Figure f1:f) totalArea+=f1.area(); return totalArea; public static void main(String args) Figure f =new Figure3;/为什么 f0=new Triangle(5.0,6.0,7.0);

26、 f1=new Rectangle(6.0,87.0); double r; try String input=JOptionPane.showInputDialog(输入圆的半径); r=Double.parseDouble(input); catch(NumberFormatException e) r=0.0; f2=new Circle(r); double totalArea=0; for(Figure f1:f) totalArea+=f1.area(); JOptionPane.showMessageDialog(null,totalarea=+totalArea); JOpti

27、onPane.showMessageDialog(null,totalarea1=+totalArea1(f); 作业:第三节 接口(interface)一、接口的概念接口是和类一种相似又有区别的一种结构,接口的设计和调用也是Java程序设计的重要技术。接口是一种特殊的抽象类接口是一种完全没有实现的类。接口用关键字interface来定义,而不是class;接口中定义的变量都是公用的最终的静态变量(即常量 用public static final 修饰);接口中没有构造方法,而且定义的方法全是抽象方法(即在方法中只提供方法的定义,而没有提供方法的实现语句),接口中的方法是用public abstract修饰的。接口采用多重继承机制,而不是采用类的单一继承机制。二、接口的声明接口的格式如下:interface 接口名 常量的定义 方法的说明三、接口的使用-类对接口的继承(实现)class 类名 implements 接口名实例一、public class InterfaceDemo public static void main(String args) /A a=new A();/接口不能生成对象 B b =new B(); b.show(); public interface A double g=9.8; void show();publ

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1