Repeat java basics (5): Object-oriented and java object-oriented

Source: Internet
Author: User

Repeat java basics (5): Object-oriented and java object-oriented

1. Encapsulation

Import java. util. date; public class Human {protected String name; protected BirthDay birthDay; protected String sex; public String getName () {return name;} public void setName (String name) {this. name = name;} public String getSex () {return sex;} public void setSex (String sex) {this. sex = sex;} public BirthDay getBirthDay () {return birthDay;} public void setBirthDay (BirthDay birthDay) {th Is. birthDay = birthDay;} public int getAge () {// encapsulation method if (birthDay! = Null) {Date nowDate = new Date (System. currentTimeMillis (); int year = nowDate. getYear () + 1900; int month = nowDate. getMonth () + 1; int day = nowDate. getDate (); int age = year-this. birthDay. getYear (); if (this. birthDay. getMonth ()> month) {return age-1;} else if (this. birthDay. getMonth () = month) {if (this. birthDay. getDay ()> = day) {return age-1 ;}} return age ;}return 0 ;}public String toString () {return "name:" + name + "sex: "+ sex +" age: "+ getAge ();} public String toString (Human human) {return this +" ";} public Human () {} public Human (String name, String sex) {super (); this. name = name; this. sex = sex;} public Human (String sex) {super (); this. sex = sex;} public Human (String name, String sex, int year, int month, int day) {super (); this. name = name; this. birthDay = new BirthDay (year, month, day); this. sex = sex ;}}
public class BirthDay {    private int year;        private int month;        private int day;    public int getYear() {        return year;    }    public void setYear(int year) {        this.year = year;    }    public int getMonth() {        return month;    }    public void setMonth(int month) {        this.month = month;    }    public int getDay() {        return day;    }    public void setDay(int day) {        this.day = day;    }        public BirthDay(){            }    public BirthDay(int year, int month, int day) {        super();        this.year = year;        this.month = month;        this.day = day;    }    }

 

2. Inheritance

public class Woman extends Human{    public Woman(){        super("Woman");    }        public Woman(String name){        super(name,"Woman");    }        public String toString(){        return " I am Woman,"+super.toString();    }        public String toString(Woman woman){        return woman+"";    }}
public class Man extends Human{    public Man(){        super("Man");    }        public Man(String name){        super(name,"Man");    }        public String toString(){        return " I am Man,"+super.toString();    }        public String toString(Man man){        return man+"";    }    }

 

3. Polymorphism

public class TestOO {    public static void main(String[] args) {        Human man = new Man("man");        Human woman = new Woman("woman");        System.out.println(woman.toString(man));        System.out.println(man.toString(woman));    }}

Running result:

 I am Woman,name:woman sex:Woman age:0 I am Man,name:man sex:Man age:0

 

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.