java笔记-面向对象-继承、抽象类、模版设计模式

来源:互联网 发布:淘宝骗局怎么处理 编辑:程序博客网 时间:2024/05/21 15:40

面向对象:继承

/*** 继承:** 将对象与对象的共性描述抽取出来,单独进行描述,在子类中使用extends关键字;** 优点:提高了代码的复用性;** 让类与类之间产生了关系,有了这个关系,才有了多态的特性。** 必须是类与类之间有所属关系才可以继承。** java只支持单继承,不支持多继承(多继承容易带来安全隐患,多个父类中继承相同功** 能时,子类对象不知道要运行哪一个);java保留多继承机制,用另外一种形式(多实现);** 如何使用继承体系中的功能?** 先查阅父类,父类定义了体系的共性内容。** 在使用时,要创建最子类的对象。1.父类可能不能创建对象;2.子类对象可以使用更多功** 能。*/class Person{private int age;private String name;public int getAge(){return age;}public void setAge(int age){this.age=age;}public String getName(){return name;}public void setName(String name){this.name=name;}}class Student extends Person{public void study(){System.out.println("Student Sdudy!");}}class Worker extends Person{public void work(){System.out.println("worker work!");}}class jicheng{public static void main(String[] args){Student stu=new Student();Worker wor=new Worker();stu.setName("sike");wor.setName("work");System.out.println("Student:"+stu.getName()+";\tWorker:"+wor.getName()+".");stu.study();wor.work();}}

继承:子类和父类变量的特点

/*** 如果子类中出现父类中非私有的同名成员变量;** 子类访问本类中的变量用this;** 子类访问父类中的变量用super;** super的使用和this几乎一致;*/class Parents{int num=4;//该类有子类,num在哪里?参看一}class children extends Parents{int num=5;public void show(){System.out.println(super.num);//super:使用父类num}}class ExtendsDemo{public static void main(String[] args){children child=new children();//参看一:对象创建时:分别开辟子类和父类的空间,各自num存在各自类的空间。child.show();}}
final关键字

/*** final:最终,作为一个修饰符;** 修饰类,函数,变量;** 被final修饰的类不能被继承(继承打破封装性);** 为了避免继承,被子类复写;** 被final修饰的方法不能复写;** 被final修饰的变量为常量,只能赋值一次,成员变量和局部变量都可以修饰;** 常量的书写规范:所有字母大写,若由多个单词组成,单词间用"_";** 内部类定义在类中的局部位置时,该类只能访问该局部被final修饰的局部变量;*/class Demo{final int x=3;public static final double PI=3.14;//固定值,共享,全局常量;public final void show1(){};//不能复写;public void show2(){//x=5; //执行该语句提示“不能为最终变量x分配值”};}class SubDemo extends Demo{//void show1(); //该语句不能执行,提示“无法覆盖Demo中的show1()”;}class FinalDemo{public static void main(String[] args){System.out.println("world!");}}

抽象类:

/*** 多个类中含有相同功能,但该功能主体不同;** 此时可以进行向父类抽取,只抽取功能定义,而不抽取主体;** 含有抽象方法的类也有抽象类,必须用abstract修饰,抽象类不能构建对象;** 子类如果使用父类抽象方法必须重写,倘若不能重写所有父类抽象方法,** 由于子类继承父类,则子类中含有未重写的抽象方法,子类也必须为抽象类,** 需用abstract修饰。** 抽象类比一般类多了抽象方法;不能实例化。** 抽象类可以不定义抽象方法,这样做可以禁止对该类创建对象;*/abstract class Student{//类中含有抽象方法,该类为抽象类;abstract void Study();//抽象方法,无实体内容,无实体(强迫复写);}class BaseStudent extends Student{//继承抽象类;public void Study(){//重写抽象访法;System.out.println("BaseStudent!");}}class AdvanceStudent extends Student{public void Study(){//重写抽象方法;System.out.println("Advance Student!");}}class AbstractDemo{public static void main(String[] args){new BaseStudent().Study();//使用继承后的抽象方法;new AdvanceStudent().Study();//使用继承后的抽象方法;}}

抽象类应用实例:

/*** 设计员工类和经理类** 员工有三个属性(姓名,工号,工资)和一个方法;** 经理除了员工的特性外还有奖金;*/abstract class Employee{//该类中有抽象方法;private String name;private String id;private double pay;//员工在生成时,其姓名,工号,工资就已经确定,需初始化;Employee(String name,String id,double pay){this.name=name;this.id=id;this.pay=pay;}public String getName(){return name;}public void setName(String name){this.name=name;}public String getId(){return id;}public void setId(String id){this.id=id;}public double getPay(){return pay;}public void setPay(double pay){this.pay=pay;}//每个员工都有工作的能力,但具体工作内容不一样;public abstract void work();//无主体的抽象方法,在子类中需重写;public abstract void speak();//让每个员工说出自己的姓名,工号,工资等内容;}class Pro extends Employee{Pro(String name,String id,double pay){super(name,id,pay);//继承自父类,使用父类构造函数方法;}//重新定义抽象方法;public void work(){System.out.println("Pro work!");}//重新定义抽象方法;public void speak(){System.out.println("Name is "+super.getName()+",Id is "+super.getId()+",Pay is "+super.getPay()+".");}}class Manager extends Employee{private double bonus;//为私有变量提供公共访问方法;public double getBonus(){return bonus;}public void setBonus(double bonus){this.bonus=bonus;}//构造函数初始化;Manager(String name,String id,double pay,double bonus){super(name,id,pay);this.bonus=bonus;}//重新定义抽象方法;public void work(){System.out.println("Manager work!");}//重新定义抽象方法;public void speak(){//父类中私有属性通过公共方法访问;System.out.println("Name is "+super.getName()+",Id is "+super.getId()+",Pay is "+super.getPay()+",Bonus is "+this.bonus+".");}}class Demo{public static void main(String[] args){//打印普通员工信息;Pro p=new Pro("mike","开发001",9999.01);p.speak();p.work();//打印管理人员信息;Manager m=new Manager("lucy","Leader001",19999.02,10000.0);m.speak();m.work();}}

模版设计方法模式:

/*** 模版方法设计模式** 在定义功能时,功能的一部分是确定的,另外一部分是不确定的,** 而确定的部分在使用不确定的部分,此时可将不确定的部分暴漏出去,由子类来完成;*//*** 函数功能:** 获取一段程序的执行时间。*/abstract class GetTime{//该类中有一部分功能不确定;使用该类为抽象类;//该方法为确定部分,犹如模版,而runCode()部分为不确定的,犹如放在模版中的内容。public final void getTime(){long start=System.currentTimeMillis();//使用currentTimeMills()函数获得系统当前运行时间;//该部分为不确定部分,定义为抽象方法,交给子类重写;runCode();//程序运行代码;long end=System.currentTimeMillis();//程序运行结束,获得程序运行结束时间;System.out.println("run code time is "+(end-start)+".");}public abstract void runCode();//该功能不确定,使用抽象方法,可在子类中重写;}class SubTime extends GetTime{//重写的抽象类中的方法;public void runCode(){for(int x=0;x<3000;x++){//程序运行较快,运行该代码可延缓时间;System.out.print(x+" ");}}}class TemplateDemo{public static void main(String[] args){SubTime st=new SubTime();st.getTime();}}

接口:

/*** 接口:** 刚开始理解时,可将接口看作是一个特殊的抽象类,** 该类中的所有方法都是抽象的,反过来,若抽象类中的所有方法** 都是抽象的,则该类可通过接口的形式实现;** 特点:常见定义:常量,抽象方法;** 接口中的成员都有固定的修饰符;** 常量: public static final** 方法:public abstract** 对于接口,上述修饰符可不用,系统会自动补上,但为了可读性** 建议写上。** 无多继承,接口与接口间存在多继承,除此之外没有。*/interface Inter{public static final int num=3;//公有静态常量;public abstract void show();//抽象方法,在子类中重写;}interface InterA{public abstract void method();}class Test implements Inter,InterA{//多实现;public void show(){//重写show方法;System.out.println("Test Interface.");}public void method(){}//重写method方法;}interface InterC extends Inter,InterA{//接口与接口间的多继承,}class InterD implements InterC{//类继承自接口,需要重写接口中的抽象方法;public void show(){}//重写接口中的抽象方法;public void method(){}//重写接口中的抽象方法;}class InterfaceDemo{public static void main(String[] args){Test t=new Test();t.show();//Inter.show();//该语句不能执行;show()非静态。System.out.println(t.num);System.out.println(Inter.num);}}

接口的具体实现:

/*** 继承:所属于;** 接口:功能扩展;具体实现由子类实现;*/abstract class Student{//类中含有抽象方法;public abstract void study();//具体学习内容不祥;public void sleep(){System.out.println("Students sleep!");}}class PriStudent extends Student{public void study(){//重写抽象类中的方法;System.out.println("Study java!");}}interface Smoke{//给部分抽烟的学生提供吸烟接口public abstract void smoking();//接口中的所有方法均为抽象方法;}//新类,继承父类,实现接口;class SmokeStudent extends PriStudent implements Smoke{//重写接口方法;public void smoking(){System.out.println("帝豪!");}}class InterDemo{public static void main(String[] args){SmokeStudent ss=new SmokeStudent();ss.smoking();ss.study();}}
0 0
原创粉丝点击