Understanding of the basic structure of Java classes

Source: Internet
Author: User

1. A class is the basic constituent unit of a Java program, defines the properties and methods of an object, and is a collection of objects that have common properties and behaviors.
2. Nature of the object: attribute + behavior.
3. The basic structure of the class: Properties, methods, construction methods, inner classes, blocks. A property is a description of the data. The method is the behavior of the object. The constructor method is used to instantiate the object.
4. Method overloading:
1) Method Overloading principle: The function name is the same, the parameter type is different, the parameter number is different, the parameter order is different, in the same scope.
2) method overloads are not related to the return value type of the method, and only methods that have different return values do not form overloads.
5. Invocation of the method: Class name. method
6. Object creation: Suppose there is a class person, creating an object statement for Person p=new person ();

Here is an example of a reference to understanding
public class Cat {

public static void Main (string[] args) {

Kitty K1=new Kitty ();//Create an object K1

Kitty K2=new Kitty ("Kitty", 3);//Create an object K2, named Kitten, age 3

System.out.println ("Kitty's name:" +k1.name+k1.age+ "old");//Output Object K1 name and age
K1.play ();//Call the Play method
K1.eat ();//Call Eat method

System.out.println ("Kitty2 's name:" +k2.name+k2.age+ ");//Output Object K2 name and age
K2.play ();//Call the Play method
K2.eat ();//Call Eat method


}
}

Class kitty{

String name= "Kitten";
int age=1;

method of constructing without parameters
Kitty () {

}

A method of constructing parameters
Kitty (String Name,int age) {
This.name=name;
This.age=age;

}

How to Play
void Play () {

System.out.println ("Play dough");
}
How to Eat
void Eat () {

System.out.println ("Eat apples");
}

}

Understanding of the basic structure of Java classes

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.