第八章:类与对象
1、什么是类,什么是对象,两者关系
2、Java中怎么定义类和对象
3、构造函数的特点
4、this关键词的用法
5、static的用法
6、变量:引用变量、局部变量、实例变量、全局变量
7、修饰符的用法:public、private、default、protected及其在继承关系中的用法
1、什么是类,什么是对象,两者关系
- 类 (Class):类是对一类具有相似属性和行为的事物的抽象描述,是一种模板或蓝图。类定义了对象的状态(通过变量)和行为(通过方法)。例如,可以定义一个
Person
类,它包含人的名字、年龄等属性和一些行为方法(如walk()
、speak()
)。
the class an abstract description of a class of things with similar properties and behaviors.
- 对象 (Object):对象是类的实例化。可以通过类创建对象,对象拥有类所定义的属性和行为。每个对象都是类的一种具体体现。比如,
Person
类可以创建多个不同的Person
对象,每个对象有不同的名字和年龄。
An object is an instance of a class. I can create the Object and the owning of defining the properties and behavior.
- 类与对象的关系:类是对象的模板或蓝图,类定义了对象的属性和行为,而对象是类的实际实例。
A class defines the properties and behavior of an object, which is the actual instance of the class.
2、Java中怎么定义类和对象
- 定义类:
public class Person {
// 属性
String name;
int age;
// 方法
public void speak() {
System.out.println("Hello, my name is " + name);
}
}
- 创建对象:
public class Main {
public static void main(String[] args) {
// 创建对象
Person person = new Person();
// new 关键字创建对象Person, person引用变量
person.name = "John";
person.age = 25;
person.speak(); // 输出: Hello, my name is John
}
}
3、构造函数的特点
- 构造函数是用于初始化对象的特殊方法。构造函数在创建对象时自动调用,用于为对象的实例变量赋初值。
- 特点:
public class Person {
String name;
int age;
// 构造函数
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void speak() {
System.out.println("Hello, my name is " + name);
}
}
-
- 构造函数的名称必须与类名相同。
-
- 构造函数没有返回类型(连
void
都没有)。
- 构造函数没有返回类型(连
-
- 如果没有显式定义构造函数,Java会自动提供一个默认构造函数(无参构造)。
-
- 可以定义多个构造函数(构造函数重载),根据不同的参数来创建对象。
4、this
关键词的用法
this
是一个指向当前对象的引用。
- 常见用途:
-
- 区分实例变量和局部变量:当参数名与实例变量相同时,可以通过
this
关键字来区分。
public class Person {
String name;
public Person(String name) {
this.name = name; // 区分实例变量和参数
}
}
-
- 调用当前类的方法:可以用
this
来调用当前类的其他方法。
- 调用当前类的方法:可以用
-
- 调用当前类的构造函数:使用
this()
来调用当前类的其他构造函数。
public class Person {
String name;
int age;
public Person(String name) {
this(name, 30); // 调用另一个构造函数
// 由第一个构造函数去调用第二个构造函数
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
5、static
的用法
static
关键字用于声明类的成员为静态的,静态成员属于类本身,而不属于类的实例(对象)。
- 常见用途:
-
- 静态变量:静态变量被所有对象共享,只有一份存储在内存中。
public class Counter {
static int count = 0; // 静态变量
public Counter() {
count++; // 每次创建对象时,count增加
}
}
-
- 静态方法:静态方法只能访问静态变量和静态方法,不能访问实例变量和实例方法。
public class MathUtil {
static int add(int a, int b) {
return a + b;
}
}
-
- 静态块:静态块用于在类加载时初始化静态成员。
public class MyClass {
static {
// 静态块
System.out.println("Class loaded.");
}
}
6、变量:引用变量、局部变量、实例变量、全局变量
- 引用变量:指向对象的变量。例如,
Person p = new Person()
中,p
是引用变量。
Person person = new Person();
// person 这个地方是一个引用变量
- 局部变量:在方法内部声明的变量,只在方法执行期间有效。局部变量没有默认值,必须显式初始化。
public void printName() {
String name = "John"; // name 是局部变量
System.out.println(name);
}
- 实例变量:属于类的实例,每个对象有独立的实例变量,通常在类中定义,且没有
static
修饰符。实例变量有默认值。
public class Person {
int age; // age 是实例变量
}
- 全局变量:这个术语不太精确,但通常指的是类的实例变量或静态变量,因其可以在类的任何方法中访问。
public class GlobalExample {
static int globalVar = 10; // 类级别的全局变量
}
7、修饰符的用法:public
、private
、default
、protected
及其在继承关系中的用法
- public:可以被任何其他类访问。
-
- 在继承中,子类可以访问父类的
public
成员。
- 在继承中,子类可以访问父类的
- private:只能在当前类中访问,无法被其他类(包括子类)访问。
-
- 在继承中,子类无法直接访问父类的
private
成员,但可以通过protected
或public
方法间接访问。
- 在继承中,子类无法直接访问父类的
- default(包私有):如果没有明确指定访问修饰符,则默认为包私有。只能在同一包中的类中访问。
-
- 在继承中,子类只能访问同包中的父类的
default
成员。
- 在继承中,子类只能访问同包中的父类的
- protected:可以被同一包中的其他类访问,也可以被子类访问,甚至如果子类在不同包中。
public class Parent {
public int pubVar;
private int priVar;
protected int proVar;
int defVar; // 默认修饰符
public Parent() {
pubVar = 1;
priVar = 2;
proVar = 3;
defVar = 4;
}
}
class Child extends Parent {
public void printValues() {
System.out.println(pubVar); // 可以访问
// System.out.println(priVar); // 编译错误
System.out.println(proVar); // 可以访问
System.out.println(defVar); // 可以访问,位于同包
}
}
-
- 在继承中,子类可以访问父类的
protected
成员,即使它们在不同的包中。
- 在继承中,子类可以访问父类的
In my opinion
Please use the English to solve the problems
- Class, Object, relationship
- Class: The class has many of the property, such as the name, number, sex, high, weight and so on.
at the same time , in the class, it has many of Object , and it can be called to the property.
- Object: Object involves many of the property, by the object , we can know many of message, and define many of properties
The Class belongs to the Object, by many of Object , we can know the message of the Class, the Object is form the class
- Java define the Class and Object
we can use the key word of public/private/protect to define the Class, In the Class, we need depends on the base of number type the Object.