1. 程式人生 > >面向對象---同一package的調用

面向對象---同一package的調用

clas out sys sta 方法 聲明 info image 數據

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的調用