1.学生类的使用:
class Hello2 { public static void main(String[] args) { Student s = new Student(); s.name = "王哥"; s.age = 25; s.gender = "男"; System.out.println(s.name + "..." + s.age + "..." + s.gender); s.study(); s.sleep(); } } class Student { String name; int age; String gender; public void study() { System.out.println("学生学习"); } public void sleep() { System.out.println("学生睡觉"); } }
结果:
2.手机类的使用:
class Hello2 { public static void main(String[] args) { Phone s = new Phone(); s.brand = "华为"; s.price = 2000; System.out.println(s.brand + "..." + s.price); s.call(); s.sendMessage(); s.playGame(); } } class Phone { String brand; int price; public void call() { System.out.println("打电话"); } public void sendMessage() { System.out.println("发信息"); } public void playGame() { System.out.println("玩游戏"); } }
结果: