Java Novice Note 14 Class inheritance Example

Source: Internet
Author: User

1.Person

package Com.yfs.javase;public class Person {private String name;private int age; Private char sex;private void Privatemehtod () {System.out.println ("Call Privatemehtod () ..."); Public person () {//system.out.println ("Create Perosn object ..."); Public person (String name) {this.name = name;} Public person (String name, int age, char sex) {this.name = Name;this.age = Age;this.sex = sex;} public void Introduce () {System.out.println ("I am ..."); Public String toString () {return ' name: ' + name + ' Age: ' + ages + ' gender: ' + sex;} public void Speak () {System.out.println (name +) did you work? ");} public void Sleep () {System.out.println (name +) did you sleep? ");} public void Eat () {System.out.println (name +) did you eat it? ");} 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;} Public Char Getsex () {return sex;} public void Setsex (char sex) {this.sex = sex;}} 

2.Student

Package com.yfs.javase;//Implementing code reuse  Single inheritance public class Student extends person {//inherit personprivate int score;@ overridepublic void Introduce () {System.out.println ("I am student ..."); Overrides the method of overriding the parent class @overridepublic String toString () {return super.tostring () + "  Score:" + score;} public int Getscore () {return score;} public void SetScore (int score) {This.score = score;} Public Student () {System.out.println ("Create Student object ..."); Extension method public void study () {System.out.println (GetName () + "in learning ...");}}

3.Teacher

Package Com.yfs.javase;public class Teacher extends person {private string Job;public string Getjob () {return job;} public void Setjob (String job) {this.job = job;} @Overridepublic void Introduce () {System.out.println ("I am teacher ...");}}

4.test1

Package Com.yfs.javase;public class Demo1 {public static void main (string[] args) {person P1 = new person ();p 1.setName ("Zhang San "); Student S1 = new Student (); S1.setname ("John Doe"); Teacher T1 = new Teacher () T1.setname ("Harry");p 1.speak (); S1.speak (); S1.study (); T1.introduce ();//s1 = t1;//Type mismatch  Are the person subclass//p1.study ();  The method property of the subclass cannot be accessed//Richter replacement person P2 = s1;//the reference to the parent class is directed to the subclass object P2 = new Student ();p 2.setName ("Tom");p 2.sleep ();//The object operation is determined by the claim type//p2.study ();//Computer technician//specific execution is determined by the object  P2 what the object p2 = new Teacher ();//Polymorphic  method overrides P2.introduce ();//The method subclass of the parent class overrides the method of the parent class}}

5.test2

Package Com.yfs.javase;public class Demo2 {public static void main (string[] args) {person P1 = new person ();p 1.setName ("Zhang San "); Student S1 = new Student (); S1.setname ("John Doe"); Teacher T1 = new Teacher (); T1.setname ("Harry"); person P2 = new Student ();p 2.setName ("Tom");//change back to cast Student s2 = (Student) p2;s2.study ();//teacher t2 = (Teacher) p2;//view The type of the object GetClass () System.out.println (P2.getclass (). GetName ()); System.out.println (T1.getclass (). GetName ());//s1 = P1;P1 = S1; System.out.println (p1); System.out.println (S1), S1.setage;p 1.setSex (' Male '); System.out.println (p1); System.out.println (S1);//p1.study ();}}

Java Novice Note 14 Class inheritance Example

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.