import java.security.PublicKey;
class Student {
private String name;
private int age=9;
private String sex;
public static int count=8;
//静态代码块只执行一次
static {
count=888;
System.out.println("静态代码块");
}
{
this.name = "amy";
// count = 10;
this.age = 99;
System.out.println("实例化代码块,构造代码块,执行了");
}
public Student() {
System.out.println("不带参数的构造方法");
}
public Student(String name) {
System.out.println("带有一个参数的构造方法");
}
public Student(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public static int getCount() {
return count;
}
public static void setCount(int count) {
Student.count = count;
}
public void eat() {
// System.out.println(this.count);
// System.out.println(Student.count);
System.out.println("吃巧克力");
}
public void sleep() {
this.eat();
System.out.println(this.name + "睡觉");
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
public static void staticFunc() {
//静态方法里面不能用this
System.out.println("dkpfkdmfklmdvsfjdksmlvdsfmlvfsjkfjkldk ");
}
}
public class ZaoFengZhuang {
public static void main(String[] args) {
func2(new Student());
new Student().eat();
new Student().eat();
new Student().eat();
Student stu=new Student();
new Student().eat();
new Student().eat();
new Student().eat();
func2(stu);
}
private static void func2(Student student) {
student.eat();
}
public static void main2(String[] args) {
Student stu = new Student();
System.out.println(stu.getAge());
System.out.println(Student.count);
// System.out.println("=======");
// Student stu2 = new Student();
}
public static void main1(String[] args) {
Student stu = new Student();
System.out.println("======");
Student stu2 = new Student("amy");
stu.setAge(18);
stu.setName("amy");
stu.setSex("男");
stu.sleep();
System.out.println(stu);
Student.staticFunc();
}
}
类和对象
最新推荐文章于 2024-09-09 23:49:04 发布