The previous article (http://blog.csdn.net/qq_32059827/article/details/51334198) made some descriptions of the abstract class, which was based on two cases to deepen the impression. Would think that abstract classes are actually not abstract:
Case one: Cat and dog case.
/* 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: Eat (); is an abstract realization: from abstraction to specific animal classes: member variables: name, age-structured method: No parameter, parameter member method: Eat (); Dog class: Inherit automatic class rewrite meal (); Cat class: Inherit the Automatic class rewriting 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.) {this.age = age;} Defines an abstract method public abstract void Eat ();} Define the specific dog class dogs extends Animal {public dog () {}public-Dog (String Name,int age) {super (name,age);//access to the parent class with a constructor method, Use the Super keyword. }public void Eat () {//Subclass method Rewrite System.out.println ("Dog eats Meat");}} Define the specific cat class Cat extends Animal {public cat () {}public cat (String Name,int age) {super (name,age);} public void Eat () {//Subclass method Rewrite System.out.println ("Cat eats Fish");}} Test class CLabstracttest {public static void main (string[] args) {//Test dog class//Specific class usage//mode 1:dog d = new Dog ();d. SetName ("Mong Choi");//inherit from parent class D.s EtAge (3);//inherit from parent class System.out.println (D.getname () + "---" +d.getage ());d. Eat ();//mode 2:dog D2 = new Dog ("Wang Choi", 3);// Super Yonder System.out.println (D2.getname () + "---" +d2.getage ());d 2.eat (); System.out.println ("---------------------------");//Use polymorphic mode animal a = new Dog () a.setname ("Mong Choi"); A.setage (3); System.out.println (A.getname () + "---" +a.getage ()); A.eat (); Animal A2 = new Dog ("Wang Choi", 3); System.out.println (A2.getname () + "---" +a2.getage ()); A2.eat (); System.out.println ("---------------------------"); a = new Dog ("Lulu", 12); System.out.println (A.getname () + "---" +a.getage ()); A.eat (); System.out.println ("---------------------------");//Exercise: Test Cat class//way a cat C = new Cat (); C.setname ("Floret"); C.setage (5); System.out.println (C.getname () + "---" +c.getage ()); C.eat (); System.out.println ("---------------------------");//Mode two cat C1 = new Cat ("Xiaohua", 4); System.out.println (C1.getname () + "---" +c1.getage ()); C1.eat (); System.out.prIntln ("---------------------------");//polymorphic implementation//mode one animal a3 = new Cat () a3.setname ("Floret"); A3.setage (5); System.out.println (A3.getname () + "---" +a3.getage ()); A3.eat (); System.out.println ("---------------------------");//mode two animal a4 = new Cat ("Xiaohua", 4);//Note the way it is written is different from the way the teacher explained it. This is equivalent to A3 (re-assigned address) (because new). But using the (same variable) A3, correct System.out.println (a4.getname () + "---" +a4.getage ()); A4.eat (); System.out.println ("---------------------------"); a3 = new Cat ("Tom", 9); System.out.println (A3.getage () + "----" +a3.getname ()); A3.eat ();}}
Case two: Company staff case:
/* Implementation: Employee Class: General Employee Class: Manager class: *///Define Employee Class abstract class Employee {//name, work number, and payroll 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, the work content is different, define an abstract method, you must have a method in the subclass to override public abstract void work ();} Ordinary employee Class 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 Type Class Manager extends Employee {//bonus, manager exclusive attribute private int bonus;//bonus Bonus Public manager () {}public Manager (String Nam E,string id,int salary,int bonus) {super (name,id,salary); This.bonus = Bonus;//this represents members of this class}public void work () {//Parent workingAbstract method subclasses have methods to rewrite System.out.println ("Talk to customers about demand");} public int Getbonus () {return bonus;} public void Setbonus (int bonus) {this.bonus = bonus;}} Class AbstractTest4 {public static void main (string[] args) {//test common employee, use polymorphic to implement employee EMP = new Programmer (); Emp.setname (" Brigitte "); Emp.setid (" 001 "); emp.setsalary (8000); System.out.println (Emp.getname () + "---" +emp.getid () + "---" +emp.getsalary ()); Emp.work (); System.out.println ("-------------"), emp = new Programmer ("Brigitte", "001", 8000);//reassign address, reallocate memory space System.out.println ( Emp.getname () + "---" +emp.getid () + "---" +emp.getsalary ()); Emp.work (); System.out.println ("-------------");//Because subclasses have unique content, they do not use polymorphic mode manager m = new manager ("Xiao Wu"); M.setid ("002") ; m.setsalary (8000); M.setbonus (2000); System.out.println (M.getname () + "---" +m.getid () + "---" +m.getsalary () + "---" +m.getbonus ()); M.work (); System.out.println ("-------------");//Assign value M = new Manager ("Elina", "czbk002", 8000,2000) by construction method; System.out.println (M.getname () + "---" +m.getid () + "---" +m.getsalary () + "---" +m.getmoney ()); M.work ();}}
The object-oriented (abstract class is not abstract) of the first season of Java entry