標籤:
1.類的定義及使用:
範例代碼:
class Main{String name;int age;String sex;String address;void eat(){System.out.println("我會吃飯 ");}void run(){System.out.println("我會跑步 ");}public static void main(String[] args) {Main p;p = new Main();p.name = "張三";p.age=18;p.sex="女";p.address = "山東淄博";System.out.println(p.name+"說:"); System.out.println("我叫"+p.name ); System.out.println("性別"+p.sex); System.out.println("我今年"+p.age+"歲"); System.out.println("我住在"+p.address ); p.eat(); p.run();}}
也可以這樣寫:
package java_1;class person{String name;int age;String sex;String address;void eat(){System.out.println("我會吃飯 ");}void run(){System.out.println("我會跑步 ");}}public class Main{public static void main(String[] args) {person p;p = new person();p.name = "張三";p.age=18;p.sex="女";p.address = "山東淄博";System.out.println(p.name+"說:"); System.out.println("我叫"+p.name ); System.out.println("性別"+p.sex); System.out.println("我今年"+p.age+"歲"); System.out.println("我住在"+p.address ); p.eat(); p.run();}}
java入門的文法學習