reference data type classification
We can put the type of the class into two kinds:
L First, Java provides us with good classes, such as the scanner class, the random class, etc., these existing classes contain a lot of methods and properties that we can use.
L Second, we create our own classes, according to the definition of the class, you can include multiple methods and properties in the class for us to use.
Overview of custom data Types
In Java, we abstract real-life things into code. At this point, we can use a custom data type (class) to describe (map) things in real life.
Class, which is a reference data type
The custom type is not Java for us to provide a good type beforehand, but rather we define a reference data type to describe a thing
Definition and use of classes
The process of mapping Java code into real things is the process of defining classes.
Currently, only the properties in the class are concerned, and the methods in the class are learned in the object-oriented part.
the definition format of the class
Create a Java file that is the same as the class name
public class class Name {
data Type property name 1;
Data Type property name 2;
...
}
Code Demo:
Public class Phone { / * * attribute * * String brand; // Brand Model String color; // Color Double // Size}
The code above, is the process of creating a class, the name of the class we named the phone, the class contains three attributes (brand model, color, size size). Note that the attributes defined in the class do not have a number of requirements.
the use format of the class
Guide Package: We put all the classes in the same folder, can avoid the guide package.
Create object: Data type variable name = new data type ();
Call method: Currently we define a custom class that does not involve methods, just attributes (the method part of the custom class is explained in the object-oriented section)
Access attribute: Variable name. Property (This is the current way, and later the method is called to replace the direct access way to complete the access to the property.) )
Code Demo:
Public classTest { Public Static voidMain (string[] args) {//defines a variable p for the phone typePhone p =NewPhone (); /** through P, use the properties in Phone*/ //access to brand properties in PP.brand = "Apple 6s";//to access the Color property in PP.color = "White";//Accessing the Size Size property in PP.size = 5.5; System.out.println ("Phone brand for" +P.brand); System.out.println ("Phone color is" +P.color); System.out.println ("Phone size is" +p.size); }}
Custom type Considerations vs. Memory graphs
Like an array of reference types, a variable of a custom type that references a type, a direct variable when the result is an object address value
java-Base-Reference data type (Class)