Java Basics and objects

Source: Internet
Author: User

[Classes & Objects]
1, Class: A collection of individuals with a series of identical attributes (characteristics) and methods (behaviors), called classes.
A class is an abstract concept that can only say what properties a class has, and not directly assign a value to a property.
For example: Humans have a height attribute, but we cannot say that human height is 180.

2. Object: An individual that takes a specific attribute value from a class, called an object.
An object is a specific individual that is taken from a class and needs to be assigned a specific value.
For example: The human has the height attribute, then Zhang three is the human one individual, can say Zhang San's
Height of 180, at this time Zhang San is a human object.

3. The relationship between classes and objects:
A class is an abstraction of an object that is the embodiment of a class.
A class is a description of an object that is an individual (instance) of a class.
The object is generated by the class in the program.
The three main characteristics of object-oriented: inheritance, encapsulation, polymorphism.

[Creation of classes]
1. Declaring a class: [Access control character] class name {}
Note Naming conventions for class names
public class Person {}

2. Declare a property: [Access control character] Property type property name;
public String name;
public int age;

3. Declare a method: [Access Control] Return value type method name () {}
If there is no return type, use void instead.
public void Say () {
System.out.println ("I'm human, I'll talk.") ");
}


4. Constructor: A special method in the class
The ① constructor has the same name as the class
The ② constructor does not have a return type, even void!
Public person () {}
Public person (String Name,int age) {}
Note
① If there is no manual write constructor, the system provides an empty parameter construct by default
If you have a handwriting constructor, the system no longer provides NULL parameter constructs

The ② constructor does not need to be called manually, but is called automatically when the object is instantiated.

③ constructor function: assigns a default value to a class's properties when instantiating an object

④ is accustomed to writing an empty parameter construct (you can create an empty object without writing parameters) and a full parameter construct.

[Steps for using classes and objects]
① Declaration of a class
public class Person {
Public String name= "Zhang San";//property
public int age=12; Method

}

② in the main method that calls the class, use the new keyword to get to the object (instantiation of the object)
Person P=new person ();

③ use the object name. Method Name () or object name. Property names, you can call properties and methods in a class
P.say (); Calling methods
System.out.println (p.name);//Call Property


[return value of method]
① if the method has a return value, you can return the structure using return
② If the method does not return a value, you need to set the return value to void, and the method must not return
③ has a method that returns a value, which is not necessarily a return value when called.
④ a method that does not return a value and must not receive a return value when called

[Invocation of Method]
① declares a method that executes the code in the method body only when it is called
The invocation of the ② method:
>>> called in other methods of this class, using the method name (); or use this. method name ();
public void Eat () {
SYSTEM.OUT.PRINTLN ("Call Eat Method");
This.say ();
}

>>> call methods in other classes, you should first instantiate the object, and then use the object name. Method name (); call
Demo02_method dm=new Demo02_method ();
Dm.say ();

[Parameter of Method]
① when declaring a method: You can set the incoming parameter list that is required to invoke the method in (), separated by commas between multiple parameters;
public void Eat (String food,int count) {}

A list of parameters when declaring a method, called a formal parameter list;

② The parameter type passed in when calling the method must correspond to the formal parameter type one by one.
Dm.eat ("Egg", 3);

A list of parameters when invoking a method, called an argument list;

[Member variables and local variables]
① member Variable: A variable that is written directly outside the method body and declared in the class
Member variables can be used in the entire class
② Local variables: variables declared inside the method body
Local variables are only valid in the current method body, and the formal parameter lists of the methods are local variables.

③ the difference between a member variable and a local variable:
⑴ scope is different: member variables can be used throughout the class, local variables are only valid in the current method body;
⑵ initial values are different: In Java, member variables are assigned initial values by default, local variables have no default initial values
⑶ Precedence: When a member variable has the same name as a local variable, the local variable is preferred in the method
If you need to use member variables, you must use this. member variable;

[Principles of use of classes]

A complete program will be divided into three parts, basic data, various operations on the data, data testing. So the complete Java program contains three kinds:

Data classes: Classes contain only attributes and construction methods;

Data manipulation classes: No attributes and construction methods, only the various methods of writing their own data to operate.

Test class: The class of the Main method, in which class instantiation, according to different requirements, call different object methods, implement object-oriented.

Java Basics and objects

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.