简单java类
-
简单java类指的是在一个类之中,主要是由属性以及getter、setter方法所组成的类,这个类不牵扯各种复杂的运行逻辑,仅仅是滴相关数据的存储,同时类都会明确有实物类型:例如:教师程序类,地址类等。
-
对于java类,在开发中,我们会有以下的开发要求
- 类名称必须要有意义,可以明确的描述某一类实物的特征
- 类中所有的属性必须使用private封装
- 如果要进行属性的设置或者修改,必须使用setter方法来完成,调用必要要用getter方法来完成
- 对象中所有信息不允许直接在类中使用:System.out.println();直接输出,必需要使用返回给调用处来进行输出处理
- 类中可以有很多的构造方法,但必须要有一个无参构造
- 类中提供一个返回对象信息的处理方法。
-
下面我们通过一个部门类来实现一个简单java类
class Dept{//部门类 private long deptNunber;//部门编号 private String deptName;//部门名称 private String deptPlace;//部门位置 public Dept(){ //定义无参构造 } public Dept(long deptNunber,String deptName,String deptPlace){ this.deptNunber=deptNunber; this.deptName=deptName; this.deptPlace=deptPlace; } public String getInfo(){ //返回对象信息的处理方法 return "部门编号为:"+this.deptNunber+" 部门名称为:"+this.deptName+" 部门位置为:"+this.deptPlace; } //实现getter、setter方法 public void setDeptNunber(long deptNunber){ this.deptNunber=deptNunber; } public void setDeptName(String deptName ){ this.deptName=deptName; } public void setDeptPlace(String deptPlace){ this.deptPlace=deptPlace; } public long getDeptNunber(){ return deptNunber; } public String getDeptName(){ return deptName; } public String getDeptPlace(){ return deptPlace; } } public class YootkDemo10{ public static void main(String[] ages ){ Dept dept=new Dept(1001,"技术部","揭阳"); System.out.println(dept.getInfo()); } } /* 部门编号为:1001 部门名称为:技术部 部门位置为:揭阳 */
对于这个部门类,就是进行数据的保存以及数据的获取,不存在任何的运算逻辑,所以这个就是一个简单java类。
类与对象的案例分析
- 面向对象是所有初学者的难点,因为涉及的概念非常多,很多时候我们都会在这里发懵,对于简单java类也不知道如何去判断,下面我们就通过一些案例来进行一些分析
- 地址类
-
要求:编写并测试一个代表地址的Address类,地址的信息包括:国家、省份、城市、街道、邮编组成,并可以返回完整的地址信息
代码实现:
class Address{//地址信息类 private String country;//国家 private String province;//省份 private String city;//城市 private String street;//街道 private long code;//邮编 public Address(){ } public Address(String country,String province,String city,String street,long code){ this.country=country; this.province=province; this.city=city; this.street=street; this.code=code; } public String getInfo(){ return "国家为:"+this.country+"、省份为:"+this.province+"、城市为:"+this.city+"、街道为:"+this.street+"、邮编为"+code; } public void setCountry(String country){ this.country=country; } public void setProvince(String province){ this.province=province; } public void setCity(String city){ this.city=city; } public void setStreet(String street){ this.street=street; } public void setCode(long code){ this.code=code; } public String getCountry(){ return this.country; } public String getProvince(){ return this.province; } public String getCity(){ return this.city; } public String getStreet(){ return this.street; } public long getCode(){ return this.code; } } public class YootkDemo11{ public static void main(String[] ages){ Address address=new Address("中国","广东","揭阳","榕城",52200); System.out.println(address.getInfo()); } } /* 程序的执行结果为: 国家为:中国、省份为:广东、城市为:揭阳、街道为:榕城、邮编为52200 */
对于这样的程序开发,定义的就是一个简单java类,程序只是进行了数据的存储。
- 员工信息类
-
要求:定义并测试一个Employee类,员工的属性包括:编号、姓名、基本薪水、薪水增长率,还包括计算薪水增长额增长额后工资总额的操作方法,同时类中提供四个构造方法
- 无参构造:编号定义为1000,姓名为:无名氏、其他内容为默认值
- 单参构造:传递编号,姓名为:新员工,基本薪水为:3000,薪水增长率为:1%
- 三参构造:传递编码。姓名,基本工资。薪水增长率为:5%
- 四参构造:所有的属性都进行传递
-
代码实现:
class Employee{//雇员类 private long nunber; private String name; private double salary; private double rate; public Employee(){ //使用构造方法互调用 this(1000,"无名氏",0.0,0.0); } public Employee(long nunber){ this(nunber,"新员工",3000,0.01); } public Employee(long nunber,String name,double salary){ this(nunber,name,salary,0.05); } public Employee(long nunber,String name,double salary,double rate){ this.nunber=nunber; this.name=name; this.salary=salary; this.rate=rate; } public double sunSalary(){ this.salary=this.salary*(1+this.rate); return this.salary; } public String getInfo(){ return "员工的编码为:"+this.nunber+"、姓名为:"+this.name+"、基本工资为"+this.salary+"、增长率为:"+this.rate; } public void setNunber(long nunber){ this.nunber=nunber; } public void setName(String name){ this.name=name; } public void setSalary(double salary){ this.salary=salary; } public void setRate(double rate){ this.rate=rate; } public long getNunber(){ return this.nunber; }public String getName(){ return this.name; } public double getSalary(){ return this.salary; } public double getrate(){ return this.rate; } } public class YootkDemo12{ public static void main(String[] ages){ Employee employee1=new Employee(); Employee employee2=new Employee(1001); Employee employee3=new Employee(1002,"Andy",3500); Employee employee4=new Employee(1003,"浩鑫",4500,0.1); System.out.println(employee1.getInfo()); System.out.println("【增长后的工资为:】"+employee1.sunSalary()); System.out.println(employee2.getInfo()); System.out.println("【增长后的工资为:】"+employee2.sunSalary()); System.out.println(employee3.getInfo()); System.out.println("【增长后的工资为:】"+employee3.sunSalary()); System.out.println(employee4.getInfo()); System.out.println("【增长后的工资为:】"+employee4.sunSalary()); } } /* 程序的执行结果为: 员工的编码为:1000、姓名为:无名氏、基本工资为0.0、增长率为:0.0 【增长后的工资为:】0.0 员工的编码为:1001、姓名为:新员工、基本工资为3000.0、增长率为:0.01 【增长后的工资为:】3030.0 员工的编码为:1002、姓名为:Andy、基本工资为3500.0、增长率为:0.05 【增长后的工资为:】3675.0 员工的编码为:1003、姓名为:浩鑫、基本工资为4500.0、增长率为:0.1 【增长后的工资为:】4950.0 */
本程序的核心结构还是简单java类,但这里面却又一些程序的逻辑结构存在,所以这个不算是一个完全的简单Java类。
- 用户登录类
-
要求:设计一个表示用户的User类,类中的变量有用户名和密码,获取和设置口令的方法和返回类型信息的方法,同时编写一个进行登录处理操作的处理逻辑,类中定义3个构造方法
- 无参构造:默认值
- 单参构造:为用户名赋值
- 双参构造,为用户名和密码赋值
-
代码实现:
class User{//用户登录类 private String name; private String code; public User(){ this(null,null); } public User(String name){ this(name,null); } public User(String name,String code){ this.name=name; this.code=code; } public String getInfo(){ return "输入的用户名为:"+this.name+"、输入的用户密码为:"+this.code; } public boolean isLogin(String testName,String testCode){ //判断两个字符串是否一致 return this.name.equals(testName) && this.code.equals(testCode) ; } public void setName(String name){ this.name=name; } public void setCode(String code){ this.code=code; } public String getName(){ return this.name; } public String getCode(){ return this.code; } } public class YootkDemo13{ public static void main(String[] ages){ User user=new User("9527","Andy"); System.out.println(user.getInfo()); if(user.isLogin("9527","Andy")){ System.out.println("用户登录成功"); }else{ System.out.println("用户登录失败,用户名或密码错误"); } } } /* 程序的执行结果为: 输入的用户名为:9527、输入的用户密码为:Andy 用户登录成功 */
此时的一个程序是一个功能性的操作类,就不是一个简单java类,因为让其存储的核心on功能是处理逻辑。