1、Java的成员变量和成员方法
public class Student {
String name;
int age;
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void eat(){
System.out.println("吃饭");
}
public void sleep(){
System.out.println("睡觉");
}
public void study(){
System.out.println("学习");
}
}
2、类的使用
public static void main(String [] args){
Student stu = new Student();
System.out.println(stu.name);//null
System.out.println(stu.age);//0
stu.name = "zhangsan";
stu.age = 18;
System.out.println(stu.name);
System.out.println(stu.age);
stu.eat();
stu.sleep();
stu.study();
}
3、一个对象调用的内存图
4、两个对象调用的内存图
5、对象产生引用关系的内存图
6、类作为参数进行使用
public static void main (String [] args){
Student stu = new Student();
stu.name = "xiaoming";
stu.age = 19;
classMethod(stu);
}
//类的成员方法不加static关键字(供外部调用),本类调用的需要加static关键字
public static void classMethod(Student stu){
System.out.println(stu.name);
System.out.println(stu.age);
}
调用过程-内存图
7、对象作为返回值
public static void main (String [] args){
Student stu = classReturn();
System.out.println(stu.name);
System.out.println(stu.age);
}
public static Student classReturn(){
Student stu = new Student();
stu.name = "zhangsss";
stu.age = 25;
return stu;
}
调用过程-内存图