【1】
1 class Employee{ 2 private String stuno; 3 private String name; 4 private float wage; 5 private float wisepay; 6 public Employee(){ 7 8 } 9 public Employee(String stuno,String name,float wage,float wisepay){ 10 this.setStuno(stuno); 11 this.setName(name); 12 this.setWage(wage); 13 this.setWisepay(wisepay); 14 } 15 public void setStuno(String s){ 16 stuno=s; 17 } 18 public void setName(String n){ 19 name=n; 20 } 21 public void setWage(float w){ 22 wage=w; 23 } 24 public void setWisepay(float p){ 25 wisepay=p; 26 } 27 public String getStuno(){ 28 return stuno; 29 } 30 public String getName(){ 31 return name; 32 } 33 public float getWage(){ 34 return wage; 35 } 36 public float getWisepay(){ 37 return wisepay; 38 } 39 public float increase(){ 40 float increase=(wisepay-wage)/wage; 41 return increase; 42 } 43 public float sum(){ 44 float sum=wage*(1+((wisepay-wage)/wage)); 45 return sum; 46 } 47 }; 48 public class Java302 { 49 public static void main(String[] args) { 50 Employee stu=null; 51 stu=new Employee("A113257","张三",6000.0f,7500.0f); 52 System.out.println("员工编号:"+stu.getStuno()); 53 System.out.println("员工姓名:"+stu.getName()); 54 System.out.println("基本薪水:"+stu.getWage()); 55 System.out.println("薪水增长额:"+stu.increase()); 56 } 57 }
程序运行结果
【2】
1 class Dog{ 2 private String name; 3 private int age; 4 private String colour; 5 public Dog(){ 6 7 } 8 public Dog(String name,int age,String colour){ 9 this.setName(name); 10 this.setAge(age); 11 this.setColour(colour); 12 } 13 public void tell(){ 14 System.out.println("姓名:"+getName()+" 年龄:"+getAge()+" 颜色:"+getColour()); 15 } 16 public String getName(){ 17 return name; 18 } 19 public void setName(String n){ 20 name=n; 21 } 22 public int getAge(){ 23 return age; 24 } 25 public void setAge(int a){ 26 age=a; 27 } 28 public String getColour(){ 29 return colour; 30 } 31 public void setColour(String c){ 32 colour=c; 33 } 34 }; 35 public class Java303{ 36 public static void main(String[] args) { 37 Dog pet=new Dog("宝马",3,"黑色"); 38 pet.tell(); 39 } 40 }
程序运行结果
【3】
1 class User{ 2 private String name; 3 private int word; 4 private int count; 5 public User(){ 6 7 } 8 public User(String name){ 9 this.setName(name); 10 } 11 public User(String name,int word,int count){ 12 this.setName(name); 13 this.setWord(word); 14 this.setCount(count); 15 } 16 public void tell(){ 17 System.out.println("用户名:"+getName()+" 口令:"+getWord()+" 用户个数:"+getCount()); 18 } 19 public String getName(){ 20 return name; 21 } 22 public void setName(String n){ 23 name=n; 24 } 25 public int getWord(){ 26 return word; 27 } 28 public void setWord(int w){ 29 if(w>10&&w<100){ 30 word=w; 31 } 32 } 33 public int getCount(){ 34 return count; 35 } 36 public void setCount(int c){ 37 c=1; 38 count=c; 39 } 40 }; 41 public class Java304{ 42 public static void main(String[] args) { 43 User use=new User("李四",51,1); 44 use.tell(); 45 } 46 }
程序运行结果