标签:
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(); } }
标签:
原文地址:http://www.cnblogs.com/yspworld/p/4334935.html