Java class definition and class instantiation

Java class definition and class instantiation

Class definition

Direct 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;
    }
}

Define a simple class

[修饰符] class 类名
{
    (零个到多个)构造器...
    (零个到多个)成员变量(属性)...
    (零个到多个)方法...
}

Define a member variable

[修饰符] 类型 成员变量名 [=默认值];
//例如
boolean isMale;
String color = "Blue";

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 specific details of class design will be expanded one by one, as well as the internal classes and code blocks not involved. Later, we will learn from these three parts for the time being.

Class instantiation

The process of constructing objects belonging to a class through a class is called class instantiation. Objects are concrete things, also known as instances, which can call instance variables and methods defined in classes. (regardless of static modification variable)

Create and use objects:

//使用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());

The performance of the process of creating objects in memory

Class is a reference data type: that is, the reference variable in the stack memory is not the member variable of the real stored object, but its reference. The actual member variable is hidden in the heap memory, which is similar to the array type mentioned above. Moreover, you can know that the stack memory stores the address value of the actual object in the heap memory, and you can directly print the value verification of the reference variable cat.

//创建对象,并初始化
Cat cat = new Cat();
//访问实例变量
cat.name = "sink";
cat.isMale = true;

Java does not allow direct access to objects in heap memory. You can only operate the object through its reference.

In addition, the same object in heap memory can be pointed to by multiple reference variables in stack memory. For example:

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

At this time, both cat and othercat are created in the stack memory, and they both point to the heap memory originally pointed to by cat, so they operate on the same actual object.

Reference book: Crazy Java handout

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>