Java polymorphism with abstract factory ----------- Rookie

xiaoxiao2021-03-06  39

Win2000 AdvSerJcreator Pro 2.5JDK 1.4.2 ================================ / * * @ (#) clsdyna.java 1.0 04/12/25 * * You can modify the template of this file in the * directory ../JCreator/Templates/Template_1/Project_Name.java * * You can also create your own project template by making a new * folder in the Directory ../jcreator/template/ Templates as Examples. * * / package myprojects.clsdyna; import java.util. *;

Class clsdyna {public clsdyna () {}

Public static void main (string args []) {system.out.println ("Starting Clsdyna ..."); Person Pr = New Boy ("Wanqing"); Person PG = New Girl ("Li Min"); System.out.println (pr.tostring ()); system.out.println (pg.tostring ()); system.out.println ("==========="); PersonFactory PC = New PersonFactory ("Wu Bo", True, 33); Person TC = pc.getPerson (); system.out.println (tc.getname ()); system.out.println (tc.toString ());}}} Abstract class person {protected String name; protected boolean sex; protected int age; protected int id; private static int pid; static {pid = 1000;} public static int PersonID () {return pid ;} public person (String na, boolean sx , INT AG) {/ * // IT Also Can Write Like this name = na; sex = sx; agent = ag; ID = person.Personid (); * / this (na, ag); if (! sx) this .SetSex (FALSE);} public person (String Na, Int Ag) {Name = NA; AGE = AG; SEX = true; // this (na); id = person.Personid ();} public Person (String Na ) {Name = na; agent = 18; sex = tru e; id = person.Personid (); public int getId () {return id;} public string getname () {return name;} public void setname (String Na) {name = na;} public int getage ()} PUBLIC INT GETAGE ()} Return Age;} public void setage (INT DG) {AGE = DG;} public boolean issex () {return sex;} Protected void setsex (boolean sx) {sex = sx;} public string toString () {returni ID: ID: " ID "; Name: " Name "; Age: " Age "; SEX: " (SEX?" Men ":" Female ");} Abstract Void Sayhello ();} Class Boy Extends Person {

Public Boy (String Na, Int Age) {Super (NA, AGE); this.SetSex (True);} public boy (String Na) {Super (NA); this.setsex (TRUE);} public void SayHello () {System.Out.println (this.tostring ());}} Class Girl Extends Person {Public Girl (String Na, Int Age) {Super (NA, AGE); this.setsex (false);} public girl (String Na) {super (na); this.setsex (false);} public void syhello () {system.out.println (this.tostring ());}

} Class personFactory {protected person pBoyGirl; public personFactory (String na, boolean sx, int ag) {if (sx) pBoyGirl = new boy (na, ag); else pBoyGirl = new girl (na, ag);} public person GetPerson () {Return PBOYGIRL;

} ============================ I have just learned Java in a few days, please ask GGJJDDMM more than a brother, Willing to grow QQ together; please specify: Java Studyqqqqqqqqqqqqqqqqqqqqqqqqqq 33349170

转载请注明原文地址:https://www.9cbs.com/read-76676.html

New Post(0)