Create class student for inheriting class person

Source: Internet
Author: User
To create a class that inherits the class person student class person//Create the parent person, {//define variables in the parent class, first name, gender sex, age.
    String name;
    String sex;

int age;
    The void GetInfo (String n,string s,int a)//defines method GetInfo gets information about the parent class.
        {name=n;
        Sex=s;
    Age=a;

    The void Showinfo ()//Definition Method Showinfo Displays the information of the parent class.
        {System.out.println ("name:" +name);
        System.out.println ("Sex:" +sex);
    System.out.println ("Age:" +age);
 The public class Student extends person//creates a subclass Student inherits the parent class person. {//defines a variable in a subclass.
        Number ID, school school, total score Zongfen, rank p.
        String ID;
        String School;
        int Zongfen;
        int p;
            void SetInfo (String num,string school_name,int zf,int pm)//define method SetInfo get information about subclasses {id=num;
            School=school_name;
            ZONGFEN=ZF;
        P=PM;
            The void Outputinfo ()//Definition Method Outinfo display the information of the subclass {System.out.println ("school:" +school);
            System.out.println ("School No.:" +id);
           System.out.println ("Name:" +name); System.out.println ("Sex:" +sex);
            System.out.println ("Age:" +age);
            SYSTEM.OUT.PRINTLN ("Total score:" +zongfen);
        SYSTEM.OUT.PRINTLN ("Class rank:" +p); The public static void main (string[] args) {//TODO auto-generated the method stub System.out.println ("
        Output the first student's information ");
        Student Zhang;
        Zhang=new Student ();//Zhang.name= "John";
        zhang.sex= "Male";
        zhang.age=23;

        Zhang.showinfo ();
        System.out.println ("Output second student's information");
        Student li=new Student ();
        Li.getinfo ("Li Ling", "female", 20);
        Li.setinfo ("101", "Guizhou University", 689, 1);
        Li.outputinfo ();
     Lines 48th through 60th: Create the object of the student class and invoke its associated member variables and member methods through the object.
 }

}

The results of the operation are as follows:

Knowledge Points:
1, the implementation of the inheritance.
2 parent classes and subclasses.
Summary of Knowledge points:
1, inheritance means that a new class inherits from a parent class, which has both a partial attribute of the parent class and a new attribute that makes the class both similar and distinct from the parent class.
2, parent classes and subclasses: Inheritance is actually the process of a new class extending itself to an existing class. The extended class is usually called the parent class, and the expanded class is called a subclass, such as: this practice, the parent is person, the subclass is student, the subclass student inherits the parent class person member variables and methods, and expands its own member variables and methods on this basis.
3, inheritance,
In Java, inheritance is implemented through keyword extends, in the following form:
Class subclass name Extents parent class name
{
Class Body
}
4, there are problems in this practice.
1, whether a subclass can be a parent of another class.
2, whether the properties of the parent class can be passed.

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.