package com.hanqi.test; public class People { private String nema;//姓名 private int age;//年齡 private String sex;//性別 private double height;//身高 //創建所有屬性的構造方法 public String getNema() { return nema; } public void setNema(String nema) { this.nema = nema; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } //成員方法 public String getSay() { return "你好"; } public int getAccount(int a,int b)//計算a+b返回int { return a+b; }
People pp=new People(); pp.setNema("張三"); pp.setSex("男"); pp.setAge(18); pp.setHeight(1.80); System.out.println("姓名:"+pp.getNema()+"\r性別:"+pp.getSex()+"\r年齡:"+pp.getAge()+"\r身高"+pp.getHeight() +"\r他說:"+pp.getSay()+"\r23+45="+pp.getAccount(23, 45) ); pp.setNema("李四"); System.out.println("姓名改為:"+pp.getNema()); }