[零基础学JAVA]Java SE面向对象部分-19.面向对象高级(07)
上季内容回顾:设计、适配器设计
抽象类和接口的区别
本季主要知识点:
本季以题目讲解为主,具体的讲解了抽象类和接口的实践使用及典型的实例分析。
练习题一
abstractclassEmployee
{
privateStringname;
privateintage;
privateStringsex;
publicEmployee(){}
publicEmployee(Stringname,intage,Stringsex)
{
this.setName(name);
this.setAge(age);
this.setSex(sex);
}
publicvoidsetName(Stringname)
{
this.name=name;
}
publicvoidsetAge(intage)
{
this.age=age;
}
publicvoidsetSex(Stringsex)
{
this.sex=sex;
}
publicStringgetName()
{
returnthis.name;
}
publicintgetAge()
{
returnthis.age;
}
publicStringgetSex()
{
returnthis.sex;
}
//显示数据
publicabstractStringgetInfo();
}
classManagerextendsEmployee
{
//职务
privateStringjob;
//年薪
privatefloatincome;
publicManager(){}
publicManager(Stringname,intage,Stringsex,Stringjob,floatincome)
{
super(name,age,sex);
this.setJob(job);
this.setIncome(income);
}
publicvoidsetJob(Stringjob)
{
this.job=job;
}
publicvoidsetIncome(floatincome)
{
this.income=income;
}
publicStringgetJob()
{
returnthis.job;
}
publicfloatgetIncome()
{
returnthis.income;
}
publicStringgetInfo()
{
return管理层信息:+\n
+\t-名称:+super.getName()+\n
+\t-年纪:+super.getAge()+\n
+\t-性别:+super.getSex()+\n
+\t-职务:+this.getJob()+\n
+\t-年薪:+this.getIncome();
}
}
classWorkerextendsEmployee
{
privateStringdept;
privatefloatsalary;
publicWorker(){}
publicWorker(Stringname,intage,Stringsex,Stringdept,floatsalary)
{
super(name,age,sex);
this.setDept(dept);
this.setSalary(salary);
}
publicvoidsetDept(Stringdept)
{
this.dept=dept;
}
publicvoidsetSalary(floatsalary)
{
this.salary=salary;
}
publicStringgetDept()
{
returnthis.dept;
}
publicfloatgetSalary()
{
returnthis.salary;
}
publicStringgetInfo()
{
return职工信息:+\n
+\t-名称:+super.getName()+\n
+\t-年纪:+super.getAge()+\n
+\t-性别:+super.getSex()+\n
+\t-部分:+this.getDept()+\n
+\t-月薪:+this.getSalary();
}
}
publicclassDemo01
{
publicstaticvoidmain(Stringargs)
{
Employeew=newWorker(张三,30,男,技术部,5000.0f);
Employeem=newManager(李四,35,女,司理,10000.0f);
System.out.println(w.getInfo());
System.out.println(m.getInfo());
}
}
redking.blog.51cto/attachment/200902/9/27212_1234192368NGtT.png
练习题二(重点来咯~~~)
redking.blog.51cto/attachment/200902/9/27212_12341923703d9L.png
分析:
只要是宠物则必定可以向宠物商店中加入
猫--gt;宠物
狗--gt;宠物
宠物商店寄存宠物
redking.blog.51cto/attachment/200902/9/27212_1234192371xfhL.png
五种宠物?如果说不是五种,可能是更多种了,那该如何?
5种宠物--gt;5个接口目标--gt;目标数组
//宠物
interfacePet
{
}
//宠物商店
classPetShop
{
}
//小狗
classDogimplementsPet
{
}
//小猫
claSSCatimplementsPet
{
}
宠物信息:
·名称
·年纪
·色彩
·价格
还应该具有一个回来全面信息的方法。
我们持续看下面哈~
//宠物
interfacePet
{
//回来宠物的名称
publicStringgetName();
//回来宠物的年纪
publicintgetAge();
//回来宠物的色彩
publicStringgetColor();
//回来宠物的价格
publicfloatgetPrice();
//回来宠物的全套信息
publicStringgetInfo();
}
//宠物商店
classPetShop
{
//有必要有一个目标数组可以保留全面的宠物
privatePetp=null;
//有必要定义一个当时已经加到了多少个宠物
privateintfoot=0;
//目标数组的大小,可以由程序运行时动态分配
//len表明目标数组的长度
publicPetShop(intlen)
{
//动态得拓荒了目标数组空间
this.p=ne
[零基础学JAVA]JavaSE面向目标部分-19.面向目标高档(07).doc
(A recap of last season's content:
Design, Adapter Design
Difference between abstract class and interface
Main points of knowledge this season:
This season is mainly based on topic explanations, and concretely explains the practical use of abstract classes and interfaces and the analysis of typical examples.
Exercise one
abstractclassEmployee
{
privateStringname;
privateintage;
privateStringsex;
publicEmployee(){}
publicEmployee(Stringname,intage,Stringsex)
{
this.setName(name);
this.setAge(age);
this.setSex(sex);
}
publicvoidsetName(Stringname)
{
this.name=name;
}
publicvoidsetAge(intage)
{
this.age=age;
}
publicvoidsetSex(Stringsex)
{
this.sex=sex;
}
publicStringgetName()
{
returnthis.name;
}
publicintgetAge()
{
returnthis.age;
}
publicStringgetSex()
{
returnthis.sex;
}
//Display Data
publicabstractStringgetInfo();
}
classManagerextendsEmployee
{
//job
privateStringjob;
//annual salary
privatefloatincome;
publicManager(){}
publicManager(Stringname,intage,Stringsex,Stringjob,floatincome)
{
super(name, age, sex);
this.setJob(job);
this.setIncome(income);
}
publicvoidsetJob(Stringjob)
{
this.job=job;
}
publicvoidsetIncome(floatincome)
{
this.income=income;
}
publicStringgetJob()
{
returnthis.job;
}
publicfloatgetIncome()
{
returnthis.income;
}
publicStringgetInfo()
{
return management information: \n
\t-name:super.getName() \n
\t-Age: super.getAge() \n
\t-Sex:super.getSex() \n
\t-Job: this.getJob() \n
\t-Annual salary: this.getIncome();
}
}
classWorkerextendsEmployee
{
privateStringdept;
privatefloatsalary;
publicWorker(){}
publicWorker(Stringname,intage,Stringsex,Stringdept,floatsalary)
{
super(name, age, sex);
this.setDept(dept);
this.setSalary(salary);
}
publicvoidsetDept(Stringdept)
{
this.dept=dept;
}
publicvoidsetSalary(floatsalary)
{
this.salary=salary;
}
publicStringgetDept()
{
returnthis.dept;
}
publicfloatgetSalary()
{
returnthis.salary;
}
publicStringgetInfo()
{
return employee information: \n
\t-name:super.getName() \n
\t-Age: super.getAge() \n
\t-Sex:super.getSex() \n
\t-Part:this.getDept() \n
\t-monthly salary: this.getSalary();
}
}
publicclassDemo01
{
publicstaticvoidmain(Stringargs)
{
Employeew=newWorker(Zhang San, 30, male, technical department, 5000.0f);
Employeem=newManager(Li Si, 35, female, manager, 10000.0f);
System.out.println(w.getInfo());
System.out.println(m.getInfo());
}
}
redking.blog.51cto/attachment/200902/9/27212_1234192368NGtT.png
Exercise 2 (the key point is here~~~)
redking.blog.51cto/attachment/200902/9/27212_12341923703d9L.png
analyze:
As long as it is a pet, it must be added to the pet store
cat -- gt; pet
dog -- gt; pet
pet store storage
redking.blog.51cto/attachment/200902/9/27212_1234192371xfhL.png
Five kinds of pets? If not five, maybe more, so what?
5 pets -- gt; 5 interface targets -- gt; target array
//pet
interfacePet
{
}
//pet Shop
classPetShop
{
}
//puppy
classDogimplementsPet
{
}
//kitten
claSSCatimplementsPet
{
}
Pet Information:
·name
·age
·color
·price
There should also be a way to return comprehensive information.
Let's keep looking below~
//pet
interfacePet
{
//return the name of the pet
publicStringgetName();
//Return the age of the pet
publicintgetAge();
//return the color of the pet
publicStringgetColor();
// return the price of the pet
publicfloatgetPrice();
//The full set of information about the returned pet
publicStringgetInfo();
}
//pet Shop
classPetShop
{
//It is necessary to have a target array to hold full pets
privatePetp=null;
//It is necessary to define how many pets have been added at that time
privateintfoot=0;
//The size of the target array can be dynamically allocated by the program runtime
//len indicates the length of the target array
publicPetShop(intlen)
{
// Dynamically open up the target array space
this.p=ne
JavaSE Target-Oriented Part-19. Target-Oriented Advanced (07).doc)
页:
[1]