面向对象例子2-student
点击浏览该文件
public class student{ private String name = "me"; private int age=18; private int height=164; private int weight=120; private int num=3; public student(String m,int a,int h,int w,int n){ name = m; age=a; height=h; weight=w; num=n; } public void display(){ System.out.println("my name is "+name); System.out.println("my age is "+age); System.out.println("my height is "+height); System.out.println("my weight is "+weight); System.out.println("my xuehao is "+num); } }
class go{ public static void main(String[] args){ //内建基本数据类型,声明的同时已经分配内存空间 int i; //初始化,仅仅是给这个变量赋以一定的值 i=0; //用户自定义的数据类型,声明的同时并没有分配内存空间 student xiaoming; //初始化用户自定义类型的变量 //通过调用new操作运算符:首先分配内存存空间,然后初始化 //一个类的变量或对象(称作这个类的实例) xiaoming = new student("Mr.Ming",88,187,240,7); xiaoming.display(); System.out.println(); student zhang = new student("Miss.Z",18,168,100,1); zhang.display(); }
}
|