Java object-oriented: class design learning notes

Source: Internet
Author: User

Class is the encapsulation body that includes both data and a group of operations acting on data.
An object is an instance of a class ).
Class has encapsulation, inheritance, polymorphism and abstraction.

1. Object status: Data Member
Public class mydate {
Int year;
Int month;
Int Day;
}

2. Object behavior: Method

Public static int daysofmonth (INT year, int month) // returns the number of days in the specified year. The static method {
Switch (month) // calculate the number of days per month
{
Case 1: Case 3: Case 5: Case 7: Case 8: Case 10: Case 12: Return 31;
Case 4: Case 6: Case 9: Case 11: return 30;
Case 2: Return isleapyear (year )? 29:28;
Default: Return 0;
}
}

3. object initialization: Constructor

Public class mydate
{
Public mydate (INT y, int M, int d) // declare the constructor
{// The constructor has the same name as the class. The constructor returns an instance of the class.
Year = y;
Month = m;
Day = D;
}
}

4. Encapsulation
Encapsulate fields in an object. You can declare them as private using private at the beginning of the field definition.

Public class mydate
{
Private int year, month, day; // instance member variable
Private Static int Thisyear; // static member variable
Static // static member variable Initialization
{
Thisyear = 2009;
}
Public static int getthisyear () // static member Method
{
Return Thisyear; // access static member variables
}
}

5. Inheritance

Public class student extends person // student is a subclass of the person class
{
Private string speciality; // major, member variable added to the subclass

Public static void main (string ARGs [])
{
Person p1 = new person ("Li Xiaoming", new mydate (1979,3, 15 ));
Student S1 = new student (); // default constructor, execute the parent class constructor person ()
S1.howmany (); // The class member method can also be called through an object
System. Out. println ("P1:" + p1.tostring () + ", S1:" + s1.tostring ());
S1.set ("Wang Jiang", new mydate (1987,9, 1); // The S1 object calls the member method of the parent class
System. Out. println (S1 + "," + s1.getname () + "than" + p1.getname () + "big" + s1.olderthen (P1) + "years old ");
S1.finalize (); // inherits the destructor of the parent class
Person. Howmany ();
}

}

5. Polymorphism

(1) subclass hide parent class member variables (2) subclass override parent class member Methods

Override means that the subclass declares and implements methods of the same name in the parent class, and the parameter list is identical.

(3) subclass inherits and reloads the parent class member Method

Overload means that multiple methods in the same class can have the same name, but the parameter list must be different.

Person P = new student ();
Object OBJ = new person ();
Public int olderthen (person P)
P1.olderthen (S1)
S1.olderthen (P1)

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.