Files
javaSE-0419/day08/src/com/inmind/static02/Student.java
2026-05-16 16:27:21 +08:00

49 lines
1.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.inmind.static02;
public class Student {
//属性(成员变量)
String name;
int age;
//静态变量
static String clazz;
public Student() {
System.out.println("Student无参构造被调用");
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
//静态方法,只跟类有关
public static void show(){
System.out.println("学生的教室为:"+clazz);
//注意:静态方法中不能访问非静态的内容
/*System.out.println("学生的姓名为:"+name);
System.out.println("学生的年龄为:"+age);
getName();*/
//注意在静态方法无法使用this关键字
// System.out.println(this);
}
//成员方法(对象方法),只跟对象有关
public String getName() {
//注意:在非静态方法(成员方法)中可以访问静态内容
System.out.println(clazz);
show();
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}