Java class object and constructor, java Class Object Construction
Github address: https://github.com/lily1010/java_learn/tree/master/dogjavaobject and class
In java, everything is an object, such as an animal, which contains cats, dogs, and fish. They can run and breathe. In short, they have the common characteristics of some animals, java can classify them as one type. This is the class in java, while cats and dogs are a specific object in this class.
A cat or a dog has some actions.
When you want to call those actions, you cannot directly call them in the class. Because different animals have different behaviors, you must create a new object to call them.
Java class
Let's look at an example.
Public class Dog {String name; int age; void bark () {// Wang calls System. out. println ("Wang, don't come over");} void hungry () {// hunger System. out. println ("My Master, I'm hungry ");}}
The preceding example is described as follows:
Public is the modifier of the class, indicating that the class is a public class and can be accessed by other classes.
Class is a key word for defining a class.
Dog is the class name.
Name and age are member variables of the class, also called attributes; bark () and hungry () are functions in the class, also called methods.
Java variable type
A class can contain the following types of variables:
Local variables: the variables defined in the method or statement block are called local variables. Both the variable declaration and initialization are in the method. After the method is completed, the variable will be automatically destroyed.
Member variable: A member variable is a variable defined outside the class and method body. This variable is instantiated (memory allocation) when an object is created ). Member variables can be accessed by methods in the class and statements of specific classes.
Class variables: class variables are also declared in the class, except the method body, but must be declared as static type. Static is also a modifier.
Constructor
Note:
The method automatically executed during class instantiation is called the constructor, which does not need to be manually called. Constructor can perform initialization during class instantiation.
The constructor name must be the same as the class name, and no return value is returned.
Each class has a constructor. If no constructor is explicitly defined for the class, the Java compiler provides a default constructor for the class.
Example:
Package com. lechebang. www;/*** Created by manlili on 2016/9/21. */public class Dog {String name; int age; public Dog () {// constructor System. out. println ("I am the constructor");} void bark () {// Wang is called System. out. println ("Wang, don't come over");} void hungry () {// hunger System. out. println ("My Master, I'm hungry ");}}
Objects in Java
The object is created based on the class. In Java, use the keyword "new" to create a new object. To create an object, follow these steps:
① Declaration: declares an object, including the Object Name and object type.
② Instantiation: Use the keyword new to create an object.
③ Initialization: when an object is created using new, the constructor is called to initialize the object.
Example:
Public class Dog {String name; int age; public Dog () {// constructor System. out. println ("I am the constructor");} void bark () {// Wang is called System. out. println ("Wang, don't come over");} void hungry () {// hunger System. out. println ("Master, I'm hungry");} public static void main (String [] args) {Dog myDog = new Dog ();}}
The execution result is
Access instance variables and Methods
// Instantiate Dog myDog = new Dog ("Flower", 3); // access the member variable myDog. name through the dot sign; // access the member method myDog. bark () through the dot sign ();
For example:
Public class Dog2 {String name; int age; // constructor. No Dog2 (String name1, int age1) {name = name1; age = age1; System. out. println ("thanks to the host for adopting me");} // normal method, the return value void bark () {System. out. println ("Wang, don't come over");} void hungry () {System. out. println ("Master, I'm hungry");} public static void main (String arg []) {// The parameter passed during object creation must correspond to Dog2 myDog = new Dog2 ("Huahua", 3); String name = myDog. name; int age = myDog. age; System. out. println ("dog name:" + name + "; dog age:" + age); myDog. bark (); myDog. hungry ();}}
Running result: