面向对象---同一package的调用

时间:2021-08-06 17:42:09
package cn.xlf.test; public class Student { //静态的数据
 String name; int id; //z学号
        int age;//年龄
 String gender; int weight; computer computer; //动态的行为
        public void study(){ System.out.println(name+"在学习");//直接调用
            System.out.println("他的学号是"+id); } public void sayHello(String sname){ System.out.println(name+"向"+sname+"说:你好"); } public static void main(String[] args){ Student s1 = new Student();//声明并实例化 分配空间 //赋值
            s1.name="向良峰"; s1.id = 1731011641; s1.study(); s1.sayHello("维克士"); Student s2 = new Student(); s2.name = "zbg"; s2.age = 18; s2.study(); s2.sayHello("维克士"); } }

 

 computer方法

 

package cn.xlf.test; public class computer { String brand; int cpuSpeed;

}

 

 主函数

 1 package cn.xlf.test;  2 
 3 public class Test2{  4     public static void main(String[] args){  5         
 6         Student s1 = new Student();  7         s1.name = "向良峰";  8         s1.age = 20;  9         
10         computer c = new computer(); 11         c.brand  ="DELL"; 12         c.cpuSpeed = 2400; 13         
14         s1.computer = c; 15  System.out.println(s1.computer.brand); 16 
17     }
18 }

 

面向对象---同一package的调用