//用class关键字定义一个类,例如: public class Person { //成员变量定义 private int id; private int age = 20; //构造方法 Person(int _id,int _age){ id = _id; age = _age; } //方法定义 public int getAge(){ return age; } public void setAge(int i){ age = i; } public int getId(){ return id; } public void setId(int i){ id = i; } public static void main(String args[]){ /* Person p1 = new Person(); System.out.println("Age="+p1.age); p1.setAge(24); p1.setId(1); System.out.println("Id="+p1.id+"\nAge="+p1.age); */ Person p1 = new Person(1,20); Person p2 = new Person(2,24); System.out.println(p1.getId()+"\t"+p1.getAge()); System.out.println(p2.getId()+"\t"+p2.getAge()); } }
原文地址:https://www.cnblogs.com/nyist0/p/12354349.html
时间: 2024-11-11 06:38:22