package com.sxt;
public class SxtStu {
int id;
int age;
String sname;
public void study() {
System.out.println("学习");
}
public void kickball() {
System.out.println("踢球");
}
public static void main(String[] args) {
SxtStu s1=new SxtStu();
System.out.println(s1.id);
System.out.println(s1.sname);
s1.id=10021;
s1.sname="李";
System.out.println(s1.id);
System.out.println(s1.sname);
s1.study();
s1.kickball();
}
}
0
null
10021
李
学习
踢球
package com.sxt;
public class User {
int id;
String name;
String pwd;
public User() {
}
public User(int id) {
this.id=id;
}
public User(int id,String name) {
this.id=id;
this.name=name;
}
public User(int id,String name,String pwd) {
this.id=id;
this.name=name;
this.pwd=pwd;
}
public static void main(String[] args) {
User u=new User();
User u1=new User(1001,"李");
User u2=new User(1002,"王","123456");
}
}
package com.sxt;
public class Person {
String name;
int age;
public void show() {
System.out.println(name);
}
public static void main(String[] args) {
Person p1=new Person();
p1.age=24;
p1.name="张";
p1.show();
Person p2=new Person();
p2.age=35;
p2.name="李";
p2.show();
Person p3=p1;
Person p4=p1;
p4.age=80;
System.out.println(p1.age);
}
}
张
李
80
package com.sxt;
public class Student {
String name;
Student friend;
public static void main(String[] args) {
Student s1=new Student();
Student s2=new Student();
s1.friend=s2;
s2.friend=s1;
s1=null;
s2=null;
}
}
package com.sxt;
public class TestThis {
int a,b,c;
TestThis(){
System.out.println("正要初始化对象:"+this);
}
TestThis(int a,int b){
//TsetThis();//这样是无法调用构造方法的
this();//调用无参的构造方法,并且必须位于第一行
a=a;//这里都是指的局部变量而不是成员变量
//这样就区分成员变量和局部变量,这种情况占了this使用情况大多数
this.a=a;
this.b=b;
}
TestThis(int a,int b,int c){
this(a,b);//调用带参的构造方法,并且必须位于第一行
this.c=c;
}
void sing() {
}
void eat() {
System.out.println("当前对象:"+this);
this.sing();//调用本类中的sing()
System.out.println("吃饭");
}
public static void main(String[] args) {
TestThis hi=new TestThis(2,3);
hi.eat();
}
}
正要初始化对象:com.sxt.TestThis@6d06d69c
当前对象:com.sxt.TestThis@6d06d69c
吃饭