Java 对象和类
类与对象的关系、构造方法、创建对象、访问成员与源文件声明规则。
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.
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);
}
}2
5
falseJava 中的类
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.
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());
}
}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.
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);
}
}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.
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);
}
}99
truenull 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.
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());
}
}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.
// 文件必须叫 Employee.java
package com.example.demo; // 1. 包声明(可选,必须最先)
import java.util.List; // 2. 导入(可多个)
public class Employee { } // 3. 唯一的 public 类,与文件名同名
class Helper { } // 4. 其他非 public 类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);
}
}2
5
falseJava 中的类
类由字段(成员变量)和方法组成。字段描述「有什么」,方法描述「能做什么」。字段不加 static 就是实例字段,每个对象各有一份。
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());
}
}Tom 28
96000.0构造方法
构造方法与类同名、没有返回类型,new 时自动调用。不写构造方法时编译器会送一个无参默认构造;一旦自己写了带参构造,默认构造就消失了。
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);
}
}3,4创建对象
new 做三件事:在堆上分配内存、调用构造方法初始化、返回引用。变量本身只存引用,赋值 b = a 只是让两个变量指向同一个对象。
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);
}
}99
truenull 调用任何方法都会抛 NullPointerException —— 这是 Java 最常见的运行时错误。访问实例变量和方法
通过 对象名.成员 访问。字段可以直接读写(除非是 private),方法要用括号调用。private 成员只能在类内部访问,这是封装的基础。
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());
}
}1
42源文件声明规则
一个 .java 文件里最多只能有一个 public 类,且文件名必须与它同名。可以有多个非 public 类。package 语句在最前,import 紧随其后。
// 文件必须叫 Employee.java
package com.example.demo; // 1. 包声明(可选,必须最先)
import java.util.List; // 2. 导入(可多个)
public class Employee { } // 3. 唯一的 public 类,与文件名同名
class Helper { } // 4. 其他非 public 类