Java class definition and class instantiation

Java class definition and class instantiation

Class definition

Object-oriented programming, classes can be seen as our custom data type, then, how can more beautiful, it is very important to define more efficiently.
There are many members of the class, each of which are very critical, after all, "object-oriented" is really important in learning Java, many Java developers out of the definition of the class waiting for us to use, come on! Capture it!

Directly on the code:

package com.my.pac02;
/**
 * @author Summerday
 * @date 2019/11/26 21:40
 */
 //类名和文件名一致,且包含main方法
public class CatTest{
    //程序入口
    public static void main(String[] args) {
        //创建对象
        Cat cat = new Cat();
        //为对象的属性赋值
        cat.name = "sink";
        cat.isMale = true;
        //通过对象调用方法
        cat.sleep();
        cat.jump();
        cat.laughAt("Susan");
        System.out.println(cat.isNotMale());
        //打印引用变量的值
        System.out.println(cat);
        //创建一个新的引用变量并指向原先的对象
        Cat otherCat = cat;
        System.out.println(otherCat);
        System.out.println(otherCat.name);//"sink"
        //将cat和实际对象之间的引用消除
        cat = null;
    }
}
//定义一个Cat类
class Cat{
    //构造方法
    Cat() {
        System.out.println("cat is cute.");
    }
    //成员变量
    String name;
    int age;
    boolean isMale;
    String color = "Blue";
    //方法
    void sleep(){
        System.out.println(name+"is sleeping---");
    }
    public void jump() {
        System.out.println(name+"is jumping---");
    }
    public void laughAt(String otherName)
    {
        System.out.println(name+"is laughing at "+otherName);
    }
    //返回boolean类型的方法
    boolean isNotMale() {
        return !isMale;
    }
}

Defining a simple class

[修饰符] class 类名
{
    (零个到多个)构造器...
    (零个到多个)成员变量(属性)...
    (零个到多个)方法...
}
  • [Modifier] class can be modified when the above code is public , or final, abstract, also can be omitted, but not recommended private and protected. Reference:
    the Java classes Why not use an external private, protected modified
  • Class name naming convention: to see to know the name of justice, meaning the need for more meaningful English word composition, each word capitalized , regulate this kind of thing, it is recommended to comply! form good habit.
  • Three members of (constructors, fields, methods) defined above can be zero or more, but is zero becomes empty class, it lacks significance.

Define a member variable

[修饰符] 类型 成员变量名 [=默认值];
//例如
boolean isMale;
String color = "Blue";
  • Member variables including instance variables and class variables , static modified member variable called the class variable, and then go into detail on after static.

Define a method

[修饰符] 返回值类型 方法名(形参列表)
{
    零条到多条可执行语句组成的方法体...
}
//例如
void sleep(){
System.out.println(name+"is sleeping---");
}
public void jump() {
System.out.println(name+"is jumping---");
}
public void laughAt(String otherName)
{
System.out.println(name+"is laughing at "+otherName);
}
//返回boolean类型的方法
boolean isNotMale() {
    return !isMale;
}

Define a constructor

[修饰符] 构造器名 (形参列表)
{
    (零条到多条可执行语句组成的构造器执行体...
}
//例如
//构造方法
Cat() {
    System.out.println("cat is cute.");
}
  • The constructor for instance constructor of the class will automatically call the constructor method, that is, create an object of a class, we will be discussed later.
  • Constructor name and class name must be the same!
  • Not defined constructor, the system will provide a default constructor.

When the details of the design class, the next will start one by one, and is not related to the internal classes and the block portion, we will then learn, for the time being from three parts inquiry.

Class instantiation

Processes belonging to the class object constructor is called by the class is instantiated. Object is a specific object exists, also referred instance, can be called instance variables and methods defined in the class. (Without considering the modification of static variables)

Create an object and use the object:

//使用Cat类创建了Cat类型的对象
//并调用Cat类的构造器,返回Cat的实例,赋值给变量cat
Cat cat = new Cat();
//访问cat的实例变量name和isMale,并为他们赋值
cat.name = "sink";
cat.isMale = true;
//调用cat的方法
cat.sleep();
cat.jump();
cat.laughAt("Susan");
//输出isNotMale()方法的返回值
System.out.println(cat.isNotMale());
  • If the case is allowed to access (access rights related to the private and other keywords, aside), member variables and methods defined in a class can be invoked through the class or instance.
  • Creating objects: 类型 实例名 = new 类型(参数列表);for example:Cat cat = new Cat();
  • Calling a class member variable or method: 类.类变量或类.方法or 实例.实例变量或实例.方法, the static keyword class variables involved, but also for the time being put aside, then continue to review.
    It can be understood: the definition of the class is to create an instance of the class many of these instances have the same characteristics.

The creation of objects in memory performance

A class is a reference data types : that is, reference variables on the stack memory is not real memory object member variable, but it is a reference to the actual member variable hidden in the heap memory, which is similar to the previously mentioned been to the type of array. Moreover, it is known, the stack is stored in the memory address of the actual objects in the heap memory, you can print directly reference the value of the variable cat is verified.

  • Create Object
//创建对象,并初始化
Cat cat = new Cat();

  • Access instance variables
//访问实例变量
cat.name = "sink";
cat.isMale = true;

Java does not allow direct access to the heap memory object, the object can only operate by reference to the object.

Further, the heap of the same object can be referenced by a plurality of stack memory variable points. E.g:

//把引用变量cat赋值给另一个引用变量otherCat
Cat otherCat = cat;

At this time, cat and otherCat are created on the stack memory, and they all point to the original cat pointed piece of heap memory, so that they operate to the same actual object.

Memory heap objects without any variables point to, as it will "waste" is a period of time in Java garbage collection mechanism to release the memory occupied by the object area.
So let him early release, a direct reference to the variable assignment is null.

Reference books: "crazy Java handouts"

Guess you like

Origin www.cnblogs.com/summerday152/p/11954197.html