10.31

將「南通青鳥 IT 教育 96 班同學在 3 教室上陸老師的 Java 課」這個場景進行業務類的抽象,類中包含屬性和方法,撰寫博文;

public class Student {
    //成員變量
    String name;
    int age;
    String code;
    //構造方法
    public Student(String name, int age, String code) {

        super();
        this.name = name;
        this.age = age;
        this.code = code;
    }
    //無參構造
    public Student() {
        super();
    }
    //void listen() {
        //System.out.println(this.name + "在上課");
    //}
    String Teach() {
        return "我叫" + this.name + "我今年" + this.age + "歲" + "我的學號是" + this.code;
    }
}

。。。。。。。。。。。。。。。。
//班級類
public class Classes {
String code;

public Classes(String code) {
    super();
    this.code = code;
}

public Classes() {
    super();
}


}
。。。。。。。。。。。。。。。
//教室類
public class Classroom {
String num;

public Classroom(String num) {
    super();
    this.num = num;
}

public Classroom() {
    super();
}


}

。。。。。。。。。。。。。。
public class Teacher {
    String name;

    public Teacher(String name) {
        super();
        this.name = name;
    }

    public Teacher() {
        super();
    }

}
。。。。。。。。。。
//課程類
public class Lesson {
String leson;

public Lesson(String leson) {
    super();
    this.leson = leson;
}

public Lesson() {
    super();
}

}
。。。。。。。。。。
//學校類
public class School {
String school;

public School(String school) {
    super();
    this.school = school;
}

public School() {
    super();
}
。。。。。。。。。。。。。。。
// 測試類
class Demo {
    public static void main(String[] args) {
        // 創建對象
        Student s = new Student();
        Classes c = new Classes();
        Classroom cr = new Classroom();
        Teacher t = new Teacher();
        Lesson l = new Lesson();
        School sc = new School();
        // 賦值
        c.code = "96班";
        cr.num = "第三教室";
        t.name = "陸老師";
        l.leson = "java課程";
        sc.school = "南通青鳥IT教育";
        s.name = "張三";
        // s.listen();
        s.age = 12;
        s.code = "ss";
        // 調用方法
        System.out.println(sc.school + c.code + "同學在" + cr.num + "上" + t.name + "的" + l.leson);
        System.out.println(s.Teach());
    }
}


輸出結果:
南通青鳥IT教育96班同學在第三教室上陸老師的java課程
我叫張三我今年12歲我的學號是ss

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章