Java 对象和类

类与对象的关系、构造方法、创建对象、访问成员与源文件声明规则。

Beginner
Key Points类是模板,对象是实例;用 new 调用构造方法创建对象,对象存在堆上、变量持有引用。

Java 中的对象

An object is an instance of a class with its own state (fields) and behaviour (methods). One class can produce any number of objects, each with independent fields.

Java
public class Puppy {
    int puppyAge;

    public void setAge(int age) {
        puppyAge = age;
    }

    public int getAge() {
        return puppyAge;
    }

    public static void main(String[] args) {
        Puppy a = new Puppy();
        Puppy b = new Puppy();

        a.setAge(2);
        b.setAge(5);

        System.out.println(a.getAge());
        System.out.println(b.getAge());
        System.out.println(a == b);
    }
}
Output
2
5
false

Java 中的类

A class consists of fields and methods: fields describe what it has, methods what it does. A field without static is an instance field, one copy per object.

Java
public class Employee {
    String name;
    int age;
    double salary;

    public double yearly() {
        return salary * 12;
    }

    public static void main(String[] args) {
        Employee e = new Employee();
        e.name = "Tom";
        e.age = 28;
        e.salary = 8000;

        System.out.println(e.name + " " + e.age);
        System.out.println(e.yearly());
    }
}
Output
Tom 28
96000.0

构造方法

A constructor shares the class name, has no return type and runs on new. Without one the compiler supplies a no-arg default; once you declare any constructor, that default disappears.

Java
public class Point {
    int x;
    int y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public static void main(String[] args) {
        Point p = new Point(3, 4);
        System.out.println(p.x + "," + p.y);
    }
}
Output
3,4

创建对象

new allocates on the heap, runs the constructor and returns a reference. The variable only holds the reference, so b = a makes both point at the same object.

Java
public class Ref {
    int value;

    public static void main(String[] args) {
        Ref a = new Ref();
        a.value = 10;

        Ref b = a;
        b.value = 99;

        System.out.println(a.value);
        System.out.println(a == b);
    }
}
Output
99
true
Java has no pointer arithmetic but it does have null references. Calling any method on null throws NullPointerException, the most common runtime error in Java.

访问实例变量和方法

Access members with object.member. Fields are read and written directly unless private; methods need parentheses. private members are visible only inside the class — the basis of encapsulation.

Java
public class Access {
    private int secret = 42;
    public int open = 1;

    private int getSecret() {
        return secret;
    }

    public int peek() {
        return getSecret();
    }

    public static void main(String[] args) {
        Access o = new Access();
        System.out.println(o.open);
        System.out.println(o.peek());
    }
}
Output
1
42

源文件声明规则

A .java file may contain at most one public class, and the file name must match it. Additional non-public classes are allowed. The package statement comes first, then imports.

Text
// 文件必须叫 Employee.java
package com.example.demo;      // 1. 包声明(可选,必须最先)

import java.util.List;         // 2. 导入(可多个)

public class Employee { }      // 3. 唯一的 public 类,与文件名同名

class Helper { }               // 4. 其他非 public 类

Friends & Partners