GitHub Address: objects and classes in Https://github.com/lily1010/java_learn/tree/master/dogjava
All things in Java are objects, such as animals, cats, dogs, fish and so on, they can run, can breathe, in short, they have some common characteristics of animals, Java can be categorized as a category. This is the class in java, and a cat, a dog, is a specific object in this class.
The cat, the dog has some action behavior, is the Method.
When you want to invoke those action actions, you cannot call them directly in the class, because different animals behave differently, and you must new an object to Invoke.
Classes in Java
Let's start with an example.
public class dog{ String name; int age; void Bark () { //bark System.out.println ("bark, don't come over"); } void hungry () { //hunger System.out.println ("master, I'm hungry");} }
The above example Illustrates:
Public is a modifier of a class that indicates that the class is a common class and can be accessed by other Classes.
Class is a keyword that defines a class.
Dog is the class Name.
name, age is a member variable of a class, also called an attribute; bark (), hungry () is a function in a class, also called a method.
Java Variable type
A class can contain the following types of variables:
Local variables: variables defined in a method or block of statements are called local Variables. Both the Declaration and initialization of variables are in the method, and the variables are automatically destroyed when the method ends.
Member Variables: member variables are variables that are defined in the class, outside the method body. This variable is instantiated (allocating Memory) when the object is Created. Member variables can be accessed by methods in the class and statements of a particular class.
Class Variables: class variables are also declared in the class, outside the method body, but must be declared as static Types. Static is also one of the Modifiers.
Construction method
Precautions:
A method that is automatically executed during a class instantiation is called a construction method, and it does not require you to invoke it manually. The construction method can do some initialization work during the instantiation of the class.
The name of the construction method must be the same as the name of the class, and there is no return Value.
Each class has a constructor method. If you do not explicitly define a construction method for a class, the Java compiler will provide a default constructor for that class.
Give me a chestnut:
Package com.lechebang.www;/** * Created by Manlili on 2016/9/21. */public class dog{ String name; int age; Public Dog () { //constructor method System.out.println ("i am a constructor method"); } void Bark () { //bark System.out.println ("bark, don't come over"); } void hungry () { //hunger System.out.println ("master, I'm hungry");} }
Objects in Java
Objects are created from a class. In java, use the keyword new to create a new object. Creating an object requires the following three Steps:
① Declaration: declares an object, including the object name and object Type.
② instantiation: Use the keyword new to create an Object.
③ initialization: When you create an object with new, the constructor method is called to initialize the Object.
Give me a chestnut:
public class dog{ String name; int age; Public Dog () { //constructor method System.out.println ("i am a constructor method"); } void Bark () { //bark System.out.println ("bark, don't come over"); } void hungry () { //hunger System.out.println ("master, I'm hungry"); } public static void main (string[] Args) { dog mydog = new dog ();} }
Execution results are
Accessing instance variables and methods
Instantiate the dog Mydog = new Dog ("flower", 3);//access the member variable through the dot number mydog.name;//access the member method through the dot Mydog.bark ();
As an example:
public class dog2{ String name; int age; Constructor method, no return value Dog2 (String name1, int age1) { name = name1; Age = age1; System.out.println ("thank the Master for adopting Me") ; The normal method must have a return value of void Bark () { System.out.println ("bark, do not come over"); } void hungry () { System.out.println ("master, I'm hungry"); } public static void main (String Arg[]) { //the arguments passed when creating the object are to correspond to the constructor method parameter list Dog2 mydog = new Dog2 ("flower", 3); String name = mydog.name; int age = mydog.age; SYSTEM.OUT.PRINTLN ("dog's name is" +name+ ", dog age is" +age "); Mydog.bark (); Mydog.hungry (); }}
Operation Result:
[turn]java class objects and construction methods