(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
[Zero Basic Learning JAVA] JavaSE Target-Oriented Part-19. Target-Oriented Advanced (07).doc)