1 //类 2 public class Student { 3 String name; 4 int age; 5 //使用new关键字,必然会调用构造器 6 public Student(){}//默认构造 7 //有参构造 8 public Student(String name) 9 { 10 this.name = name; 11 } 12 public void say(){ 13 System.out.println(this.name+"在说话");//this指针的调用 14 } 15 } 16 //main 17 public class Main { 18 public static void main(String[] args){ 19 Student s = new Student(); 20 s.name = "不良帅"; 21 s.age = 300; 22 System.out.println(s.name); 23 System.out.println(s.age); 24 s.say(); 25 } 26 }
标签:21,System,面向对象,2023.7,Student,out,public,name From: https://www.cnblogs.com/muzhaodi/p/17572218.html