2016-06-29
对象是类的实例。
package com.java1995; /** * Computer类 * @author Administrator * */ public class Computer { }
package com.java1995; /** * Computer测试类 * @author Administrator * */ public class TestComputer { public static void main(String[] args) { Computer com;//栈内存,声明一个对象,com是实例的引用 com = new Computer();//堆内存,初始化一个对象 Computer com1=new Computer(); } }
package com.java1995; /** * 学生类 * @author Administrator * */ public class Student { String name; String stuno; String sex; int age; public void study(){ System.out.println("学生正在努力的学习"); } public void play(){ System.out.println("学生正在玩耍"); } }
package com.java1995; /** * 学生测试类 * @author Administrator * */ public class TestStudent { public static void main(String[] args) { Student s=new Student(); s.name="王大锤"; s.stuno="250250"; s.sex="男"; s.age=3; System.out.println("姓名 : "+s.name); System.out.println("学号 :"+s.stuno); System.out.println("性别 :"+s.sex); System.out.println("年龄 :"+s.age); s.play(); s.study(); } }
【参考资料】