4.51 Abstract class Exercises

Source: Internet
Author: User

1:

/* Cat dog Case Specific things: Cat, dog commonness: Name, age, meal analysis: from concrete to abstract Cat: Member Variable: Name, Age construction Method: No parameter, with parameter member method: Eat (cat eat fish) Dog: Member Variable: Name, Age construction Method: No parameter, with parameter member method: Eat (dog eats meat) because have common content , so it extracts a parent class. Animals. But because the content of the meal is not the same, so the method of eating is abstract, and the method is abstract class, the class must be defined as abstract class. Abstract Animal class: member Variable: Name, Age construction Method: No parameter, with parameter member method: Meal (); realization: From abstraction to concrete animal class: member Variable: name, age-structured method: No parameter, with parameter member method: Eat (); Dog class: Inherit automatic object rewrite meal (); Cat class: Inherit automatic object rewrite meal () *///Definition Abstract Animal class abstract class animal {//  name private string name;//  age Private  int age;public animal ()  {}public animal (string name, int age)  { This.name = name;this.age = age;} Public string getname ()  {return name;} Public void setname (String name)  {this.name = name;} Public int getage ()  {return age;} Public void setage (Int age)  {this.age = age;}   defines an abstract method Public abstract void eat ();}   Define a specific dog class Class dog extends animal {public dog ()  {}public dog (String  name,&nbSp;int age)  {super (name, age);} Public void eat ()  {system.out.println ("Dog eats Meat");}}   Define specific Cat class Class cat extends animal {public cat ()  {}public cat (String  name, int age)  {super (name, age);} Public void eat ()  {system.out.println ("Cat eats Fish");}}   Test class Class abstracttest {public static void main (String[] args)  {//   Test Dog class//  specific class Usage//  way 1:dog d = new dog ();d. SetName ("Mong Choi");d. Setage (3); System.out.println (D.getname ()  +  "---"  + d.getage ());d. Eat ();//  mode 2:dog d2  = new dog ("Wang Choi",  3); System.out.println (D2.getname ()  +  "---"  + d2.getage ());d 2.eat (); System.out.println ("---------------------------"); Animal a = new dog (); A.setname ("Wang Choi"); A.setage (3); System.out.println (A.getname ()  +  "---"  + a.getage ()); A.eat (); Animal a2 =&nbsP;new dog ("Wang Choi",  3); System.out.println (A2.getname ()  +  "---"  + a2.getage ()) a2.eat ();//  Exercise: Testing Cat Class}}

2:

/* Teacher Case Specific things: basic class teacher, employment class teacher common: Name, age, lectures. Analysis: Basic class teacher name, age lectures. Employment class teacher name, age lectures. Realization: Teacher class basic class teacher employment class teacher *///definition abstract Teacher class abstract class teacher {//  name private string name;//   Age Private int age;public teacher ()  {}public teacher (string name, int  age)  {this.name = name;this.age = age;} Public string getname ()  {return name;} Public void setname (String name)  {this.name = name;} Public int getage ()  {return age;} Public void setage (Int age)  {this.age = age;}   Abstract method Public abstract void teach ();}   Basic class Teacher Class Class basicteacher extends teacher {public basicteacher ()  {}public  basicteacher (string name, int age)  {super (name, age);} Public void teach ()  {system.out.println ("Basic class teacher explaining Javase");}}   Employment Class Teacher Class class workteacher extends Teacher {public workteacher ()  {}public workteacher (string name, int age)  {super (name, age);} Public void teach ()  {system.out.println ("Employment class teacher explains Java ee");}} Class abstracttest2 {public static void main (String[] args)  {//  specific class tests , Play//  Test (polymorphism)//  Basic class Teacher Teacher t = new basicteacher () t.setname ("Cheng Qing"); T.setage (30 ); System.out.println (T.getname ()  +  "---"  + t.getage ()); T.teach (); System.out.println ("--------------"); T = new basicteacher ("Cheng Qing",  30); System.out.println (T.getname ()  +  "---"  + t.getage ()); T.teach (); System.out.println ("--------------");//  Employment class teacher T = new workteacher (); T.setname ("Li"); T.setage (27); System.out.println (T.getname ()  +  "---"  + t.getage ()); T.teach (); System.out.println ("--------------"); T = new workteacher ("Li",  27); System.out.println (T.getName ()  +  "---"  + t.getage ()); T.teach ();}} 

3:

/* Student Case Specific matters: basic class students, employment class students in common: Name, age, class, study, Meal Analysis: Basic class student member variable: name, Age, class member method: Study, meal Employment class student member variable: name, Age, class member method: study, eat to get a student class. Member variables: Name, age, Class membership method: study, eat to achieve: Students class basic class student employment class students *///definition abstract Student class abstract class student {//  name private  String name;//  Age private int age;//  Class Private string grand;public  student ()  {}public student (String name, int age, string grand)  { This.name = name;this.age = age;this.grand = grand;} Public string getname ()  {return name;} Public void setname (String name)  {this.name = name;} Public int getage ()  {return age;} Public void setage (Int age)  {this.age = age;} Public string getgrand ()  {return grand;} Public void setgrand (String grand)  {this.grand = grand;}   Learning Public abstract void study ();//  dinner public Void eat ()  {system.out.println ("Learning tired, should eat");}}   Specific basic class class basicstudent extends student {public basicstudent ()  {} Public basicstudent (String name, int age, string grand)  {super (name,  Age, grand);} Public void study ()  {system.out.println ("Basic class learner is Javase");}}   Specific Employment Class class workstudent extends student {public workstudent ()  {}public  workstudent (String name, int age, string grand)  {super (Name, age,  grand);} Public void study ()  {system.out.println ("Job-class students are learning Java EE");}} Class abstracttest3 {public static void main (String[] args)  {//  Basic class Test/ The/  tests the Student s = new basicstudent () s.setname ("Brigitte") in a polymorphic manner; S.setage (" 1111 "); System.out.println (S.getname ()  +  "---"  + s.getage ()  + ---+ s.getgrand ()); S.study (); S.eat (); System.out.println ("--------------"); S = new basicstudent ("Wu Xin", 48,  "1111"); System.out.println (S.getname ()  +  "---"  + s.getage ()  +  "---" + s.getgrand ()); S.study (); s.eat ();//  Employment Class Test}}

4:

/* If we need to design an employee class when developing a system, the employee has 3 attributes: Name, work number, and salary. The manager is also an employee, and there is a bonus attribute in addition to the attributes of the employee. Use the idea of inheritance to design employee and manager classes. Requires that the class provide the necessary methods for property access. Analysis: General Employee class member variables: Name, work number, and salary. Member method: Job Manager class: member variable: name, work number, and salary, Bonus attribute member method: Work Realization: Employee Class: General Employee Class: Manager class: *///Define Employee Class abstract class employee {//name, Work number and salary private string name;private string id;private int salary;public  Employee ()  {}public employee (string name,string id,int salary)  {this.name =  name;this.id = id;this.salary = salary;} 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 int getsalary ()  {return salary;} Public void setsalary (Int salary)  {this.salary = salary;} Work public abstract void work ();} General Staff Class Class&nbsP Programmer extends employee {public programmer () {}public programmer (String name, string id,int salary)  {super (name,id,salary);} Public void work ()  {system.out.println ("Write Code as required");}} Manager Class class manager extends employee {//Bonus private int money; //bonus  Bonus Public manager () {}public manager (String name,string id,int salary,int money)  {super (name,id,salary); This.money = money;} Public void work ()  {system.out.println ("Talk to customers about demand");} Public int getmoney ()  {return money;} Public void setmoney (Int money)  {this.money = money;}} Class abstracttest4 {public static void main (String[] args)  {//  Test General Staff Employee emp = new programmer (), Emp.setname ("Cheng Qing"), Emp.setid ("czbk001"); Emp.setsalary (18000); System.out.println (Emp.getname ()  +  "---"  + emp.getid ()  +  "---"  + emp.getsalary ()); Emp.work (); System.out.println ("-------------"); Emp = new programmer ("Cheng Qing",  "czbk001",  18000); System.out.println (Emp.getname ()  +  "---"  + emp.getid ()  + --- +  Emp.getsalary ()); Emp.work (); System.out.println ("-------------");/*emp = new manager (); Emp.setname ("Li"); Emp.setid ("czbk002 "); Emp.setsalary (8000); Emp.setmoney (2000); *///because subclasses have unique content, we use subclasses to test Manager m = new manager (); M.setname ("Li"); M.setid ("czbk002"); m.setsalary (8000); M.setmoney (2000); System.out.println (M.getname ()  +  "---"  + m.getid ()  +  "---" + m.getsalary ()  +  "---"  + m.getmoney ()); M.work (); System.out.println ("-------------");//  assigns a value M = new manager ("li",  "czbk002" by constructing the method,  8000, 2000); System.out.println (M.getname ()  +  "---"  + m.getid ()  + --- +&nbsP;m.getsalary ()  +  "---"  + m.getmoney ()); M.work ();}} 


4.51 Abstract class Exercises

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.