52ky 发表于 2022-9-23 10:39:46

[零基础学JAVA]Java SE面向对象部分-17.面向对象高级(05)

上季内容回忆:
1、目标的多态性
2、笼统类和接口
本季首要知识点:
笼统类和接口的实践使用
1、子类目标可认为父类目标实例化,以后调用的方法都是子类中现已被覆写过的方法。
2、就可以使用此特性,观察笼统类的使用,由于在笼统类中有很多的笼统方法。
笼统类
abstractclassA
{
publicabstractvoidfun();
};
classBextendsA
{
publicvoidfun()
{
System.out.println(B==gt;HelloWorld);
}
};
claSSCextendsA
{
publicvoidfun()
{
System.out.println(C==gt;HelloWorld);
}
};
publicclassDemo01
{
publicstaticvoidmain(Stringargs)
{
Aa=newB();
a.fun();
}
};
abstractclassA
{
publicabstractvoidfun();
};
classBextendsA
{
publicvoidfun()
{
System.out.println(B==gt;HelloWorld);
}
};
claSSCextendsA
{
publicvoidfun()
{
System.out.println(C==gt;HelloWorld);
}
};
publicclassDemo01
{
publicstaticvoidmain(Stringargs)
{
Aa=newC();
a.fun();
}
};
笼统类是可以实例化的,通过目标的多态性来实例化
笼统类的首要作用是什么呢?笼统类即是类似于一个模板操作==gt;JAVAWEBServlet程序,提供的即是一个模板。
把上面的实践的比如变为程序
abstractclassErr
{
publicvoidprintInfo()
{
System.out.println(名字:+this.getName());
System.out.println(班级:+this.getCls());
System.out.println(事由:+this.getCau());
}
//得到名字,由详细的子类去做
publicabstractStringgetName();
//得到班级,由详细的子类去做
publicabstractStringgetCls();
//得到事由
publicabstractStringgetCau();
}
classZhangSanextendsErr
{
publicStringgetName()
{
return张三;
}
publicStringgetCls()
{
return小五班;
}
publicStringgetCau()
{
return由于上课吃工具,所以被教师抓住了,所以要填写违纪卡。;
}
}
classLiSiextendsErr
{
publicStringgetName()
{
return李四;
}
publicStringgetCls()
{
return大五班;
}
publicStringgetCau()
{
return由于上课睡觉,所以被教师抓住了,所以要填写违纪卡。;
}
}
publicclassDemo02
{
publicstaticvoidmain(Stringargs)
{
Erre=newZhangSan();
e.printInfo();
}
}
我们改成李四看看作用哈~
abstractclassErr
{
publicvoidprintInfo()
{
System.out.println(名字:+this.getName());
System.out.println(班级:+this.getCls());
System.out.println(事由:+this.getCau());
}
//得到名字,由详细的子类去做
publicabstractStringgetName();
//得到班级,由详细的子类去做
publicabstractStringgetCls();
//得到事由
publicabstractStringgetCau();
}
classZhangSanextendsErr
{
publicStringgetName()
{
return张三;
}
publicStringgetCls()
{
return小五班;
}
publicStringgetCau()
{
return由于上课吃工具,所以被教师抓住了,所以要填写违纪卡。;
}
}
classLiSiextendsErr
{
publicStringgetName()
{
return李四;
}
publicStringgetCls()
{
return大五班;
}
publicStringgetCau()
{
return由于上课睡觉,所以被教师抓住了,所以要填写违纪卡。;
}
}
publicclassDemo02
{
publicstaticvoidmain(Stringargs)
{
Erre=newLiSi();
e.printInfo();
}
}
调用模板成功了哈~
(模板设计)场景:
假定Person分为Worker和Student,工人的属性包括:名字、年纪、薪酬,学生的属性包括:名字、年纪、成果,那么如今人都可以说话,但是工人和学生说的话必定不一样。此刻,必定人中的说话方法是固定的,必定是一个普通方法,仅仅说话的内容不一样。
abstractclassPerson
{
privateStringname;
privateintage;
publicPerson(Stringname,intage)
{
this.name=name;
this.age=age;
}
publicStringgetName()
{
returnthis.name;
}
publicintgetAge()
{
returnthis.age;
}
publicvoidtalk()
{
//这是要说,内容都在此方法中
System.out.println(this.getContent());
}
publicabstractStringgetContent();
}
classStudentextendsPerson
{
privatefloatscore;
publicStudent(Stringname,intage,floatscore)
{
super(name,age);
this.score=score;
}
publicStringgetContent()
本资料共包括以下附件:
[零基础学JAVA]JavaSE面向目标部分-17.面向目标高档(05).doc

(Memories from last season:
1. The polymorphism of the target
2. Generic classes and interfaces
Top knowledge points this season:
Practical use of generic classes and interfaces
1. The subclass target can be considered as the instantiation of the parent class target, and the methods to be called in the future are the methods that have been overridden in the subclass.
2. You can use this feature to observe the use of general classes, because there are many general methods in general classes.
general class
abstractclassA
{
publicabstractvoidfun();
};
classBextendsA
{
publicvoidfun()
{
System.out.println(B==gt;HelloWorld);
}
};
claSSCextendsA
{
publicvoidfun()
{
System.out.println(C==gt;HelloWorld);
}
};
publicclassDemo01
{
publicstaticvoidmain(Stringargs)
{
Aa=newB();
a.fun();
}
};
abstractclassA
{
publicabstractvoidfun();
};
classBextendsA
{
publicvoidfun()
{
System.out.println(B==gt;HelloWorld);
}
};
claSSCextendsA
{
publicvoidfun()
{
System.out.println(C==gt;HelloWorld);
}
};
publicclassDemo01
{
publicstaticvoidmain(Stringargs)
{
Aa=newC();
a.fun();
}
};
Generic classes are instantiable and instantiated through the polymorphism of the target
What is the primary function of the general class? The general class is similar to a template operation ==> JAVAWEBServlet program, which provides a template.
Turn the above practical example into a program
abstractclassErr
{
publicvoidprintInfo()
{
System.out.println(name:this.getName());
System.out.println(class:this.getCls());
System.out.println(reason:this.getCau());
}
//Get the name, it is done by the detailed subclass
publicabstractStringgetName();
//Get the class, which is done by the detailed subclass
publicabstractStringgetCls();
// get the reason
publicabstractStringgetCau();
}
classZhangSanextendsErr
{
publicStringgetName()
{
return Zhang San;
}
publicStringgetCls()
{
return the fifth class;
}
publicStringgetCau()
{
return was caught by the teacher because he ate tools in class, so he had to fill out a disciplinary card. ;
}
}
classLiSiextendsErr
{
publicStringgetName()
{
return Li Si;
}
publicStringgetCls()
{
return the fifth class;
}
publicStringgetCau()
{
return was caught by the teacher because he slept in class, so he had to fill out a disciplinary card. ;
}
}
publicclassDemo02
{
publicstaticvoidmain(Stringargs)
{
Erre=newZhangSan();
e.printInfo();
}
}
Let's change it to Li Si and see the effect~
abstractclassErr
{
publicvoidprintInfo()
{
System.out.println(name:this.getName());
System.out.println(class:this.getCls());
System.out.println(reason:this.getCau());
}
//Get the name, it is done by the detailed subclass
publicabstractStringgetName();
//Get the class, which is done by the detailed subclass
publicabstractStringgetCls();
// get the reason
publicabstractStringgetCau();
}
classZhangSanextendsErr
{
publicStringgetName()
{
return Zhang San;
}
publicStringgetCls()
{
return the fifth class;
}
publicStringgetCau()
{
return was caught by the teacher because he ate tools in class, so he had to fill out a disciplinary card. ;
}
}
classLiSiextendsErr
{
publicStringgetName()
{
return Li Si;
}
publicStringgetCls()
{
return the fifth class;
}
publicStringgetCau()
{
return was caught by the teacher because he slept in class, so he had to fill out a disciplinary card. ;
}
}
publicclassDemo02
{
publicstaticvoidmain(Stringargs)
{
Erre=newLiSi();
e.printInfo();
}
}
Calling the template is successful~
(Template Design) Scenario:
Assuming that Person is divided into Worker and Student, the attributes of workers include: name, age, and salary, and the attributes of students include: name, age, and achievements, so now everyone can speak, but workers and students must speak differently. At this moment, the way of speaking among people must be fixed, it must be a common way, only the content of the speech is different.
abstractclassPerson
{
privateStringname;
privateintage;
publicPerson(Stringname,intage)
{
this.name=name;
this.age=age;
}
publicStringgetName()
{
returnthis.name;
}
publicintgetAge()
{
returnthis.age;
}
publicvoidtalk()
{
//This is to say, the content is in this method
System.out.println(this.getContent());
}
publicabstractStringgetContent();
}
classStudentextendsPerson
{
privatefloatscore;
publicStudent(Stringname,intage,floatscore)
{
super(name, age);
this.score=score;
}
publicStringgetContent()
This document includes the following attachments:
JavaSE Target-Oriented Part-17. Target-Oriented Advanced (05).doc)




页: [1]
查看完整版本: [零基础学JAVA]Java SE面向对象部分-17.面向对象高级(05)