面向对象案列分析

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_45176509/article/details/102699224

案列一:
定义并测试一个代表员工的Employee,员工属性包括:编号、姓名、基本薪水、薪水增长率、还包括计算薪水增长额以及计算增长后工资总额操作方法。

 class Employee   //定义类
{ 
  private String empno ;    //定义属性
  private String name;
  private double salary;
  private double rate;
  public Employee(){}    //无参构造方法
  public Employee(String empno,String name,double salary,double rate){  //有参构造
      this.empno=empno;   
      this.name=name;
    this.salary=salary;
    this.rate=rate;
    }
public double salaryIncValue(){ //得到薪水增长额度方法
	return this.salary*this.rate;
    }
public double salaryIncAll(){//得到薪水增长总额方法
    return this.salary=this.salary*(1+this.rate);
    }
//setter 方法
 public void setEmpno(String empno){
     this.empno=empno;
    }
//getter 方法
public String getEmpno(){
    return this.empno;
    }
//获取员工全部信息方法
public String getInfo(){    
    return "员工号:"+empno+"、姓名:"+name+"、基本工资:"+salary+"、工资增长率:"+rate;
    }
}
public class Java   //主类
{
    public static void main(String args[]){   //主方法
    Employee a=new Employee("123456","王姬",3500,0.20);   //实例化对象
	   System.out.println(a.getInfo());  //对象调用getInfo()方法输出员工全部信息
       System.out.println("薪水调整额度"+a.salaryIncValue());  //输出增长额
       System.out.println("薪水发放总额"+a.salaryIncAll());  //输出总额
   }
}

运行结果:
员工号:123456、 姓名:王姬、基本工资:3500.0、工资增长率:0.2
薪水调整额度700.0
薪水发放总额4200.0

案例二:
声明一个图书类,其数据成员为书名、编号(静态变量实现自动编号),书价、并拥有静态数据成员册数、记录图书册数,在构造方法中利用此静态变量为对象的编号赋值,在主方法中定义多个对象,并求出总册数。

class Book{
  private String name;
  private int bid;
  private static int count=0;
  private double price;
  public Book(){}
  public Book(String name,double price){
	  this.name=name;
	  this.price=price;
      this.bid=count++;//this.bid=bid;count++;
    }
//settr、getter省略
  public static int getCount(){
      return count;
    }
  public String getInfo(){
      return  "图书名:"+this.name+"、图书编号:"+this.bid+"、图书价格:"+this.price;
    }
 }
public class Java
{
public static void main (String args[]){
	Book b1=new Book("oracle",18.0);
    Book b2=new Book("java",17.5);
	System.out.println(b1.getInfo());
    System.out.println(b2.getInfo());
    System.out.println("图书总数:"+Book.getCount());
    }
}

输出结果:
图书名:oracle、图书编号:0、图书价格:18.0
图书名:java、图书编号:1、图书价格:17.5
图书总数:2

案例三:
设计一个表示用户的user类,类中有用户名、口令、记录用户个数的变量,定义类的三个构造方法(赋值、为用户名和口令赋值)、获取设置口令的方法和返回类信息。

class User{
    private String name;
    private String password;
    private static int count = 0;
    public User(){
    this("1","2");
	    }
   public User(String name){
    this(name,"3");
     }
  public User(String name,String password){
    this.name=name;
    this.password=password;
    count++;
    }
public static int getCount(){
	return count;
    }
//settr、getter省略
public String getInfo(){
   return "用户名:"+this.name+"、口令:"+this.password ;
    }
}
public class Java{
 public static void main (String args[]){
    User a=new User();
    User b=new User("一秋");
    User c=new User("一叶知秋","123456789");
    System.out.println(a.getInfo());
    System.out.println(b.getInfo());
    System.out.println(c.getInfo());
    System.out.println("用户个数:"+User.getCount());
    }
}

输出结果:
用户名:1、口令:2
用户名:一秋、口令:3
用户名:一叶知秋、口令:123456789
用户个数:3

猜你喜欢

转载自blog.csdn.net/weixin_45176509/article/details/102699224