员工类 Employee, 经理类:Manager

public class Employee {
private String name;
private double salary;
private LocalDate hireDay; public Employee(String n, double s, int year, int month, int day){
this.name = n;
this.salary =s;
this.hireDay = LocalDate.of(year, month, day);
} public String getName() {
return name;
} public double getSalary() {
return salary;
} public LocalDate getHireDay() {
return hireDay;
}
public void raiseSalary(double byPercent){
double raise = salary * byPercent / 100;
this.salary += raise;
}
}

class Manager extends Employee{
private double bonus;
public Manager(String n, double s, int year, int month, int day, double bonus){
super(n,s,year,month,day); //调用父类构造函数
this.bonus = bonus;
}
public void setBonus(double bonus){
this.bonus = bonus;
} @Override
public double getSalary(){
     //double baseSalary = this.salary;  //Error 'salary' has private access in 'Employee'
     //double baseSalary = this.getSalary();  //无限嵌套调用本身
double baseSalary = super.getSalary();
return baseSalary + bonus;
}
}
public class Test1 {

    public static void main(String[] args) throws InterruptedException {

        Employee boss = new Manager("boss", 1000000, 2011, 5, 1, 500000);
//boss.setBonus(600000); 编译错误,无法找到setBonus
System.out.println(boss.getName());
System.out.println(boss.getSalary());
}
}

  1. extends 关键字表示类之间的继承关系。子类继承基类中的所有方法和数据。

  2. 重写覆盖基类方法。当有些基类方法并不适用于子类时,可以重写该方法。Manager类 增加了数据bonus,在计算薪水的时候getSalary理应也要把bonus加进去。子类实例可以调用继承的基类public成员(包括方法和数据),但private成员只有Employee类自己可见。当在Manager中使用this.salary时会报 'salary' has private access in 'Employee' 的错误。this.getSalary()是调用子类函数getSalary本身。这时可以用super指代基类,调用基类方法 —> super.getSalary();

  3. 可以使用基类的变量指向子类实例,如上Employee boss = new Manager("boss", 1000000, 2011, 5, 1, 500000);  在调用方法时是实例去调用,即boss.getSalary()输出的是 1000000 + 500000。但不能调用子类独有的方法,如setBonus,会造成编译错误。

  一个对象变量可以引用多种实际类型------->多态:    上例代码 boss可以指向Manager实例,当然也可以指向Employee实例。

  4. 子类构造函数可以在第一句使用super(param)的方式显示调用基类的构造函数。如果没有显式调用,则将自动调用基类的默认构造函数,如果基类没有默认构造函数,则会编译错误。也就是说子类的构造函数总是要先执行基类构造。

  5. final 修饰变量, 变量只能初始化一次; 修饰方法,方法不能被子类重写; 修饰类, 类不能被继承。

  6. 抽象类。abstract关键字修饰抽象类和抽象方法。包含抽象方法的类本身必须被声明为抽象类。抽象类可以包含具体数据和具体方法。抽象类不能被实例化。

  7. 重写equals方法,多有类都派生于Object类,Object类的equals源码如下,直接比较引用,这对很多其他类并不适用:

public boolean equals(Object obj) {
return (this == obj);
}
  •     应该重写Object中的equals,应该定义为 public boolean equals(Object otherObject),不要改变参数类型,否则就不是重写了;
  •     if(this == otherObject) 优化,地址相等直接返回。
  •     检测otherObject是否为空, 即if(otherObject == null) return false;
  •     比较this和otherObject是否属于同类。如果equals在每个子类中都有自己的定义,则应该使用getClass判断类型。如果所有子类都有统一的语义,则可使用 if(!otherObject instanceof ClassName)return false;
  •     将otherObject转换为相应类型的变量, ClassName other = (ClassName)otherObject;
  •     比较数据,基本类型 使用 ==比较。类类型可使用Objects.equals比较。

   8. hashCode 由Object类定义的,返回一个整型值。源码:public native int hashCode(); 详细信息见下面的注释。

看源码的注释

/**
* Returns a hash code value for the object. This method is
* supported for the benefit of hash tables such as those provided by
* {@link java.util.HashMap}.
* <p>
* The general contract of {@code hashCode} is:
* <ul>
* <li>Whenever it is invoked on the same object more than once during
* an execution of a Java application, the {@code hashCode} method
* must consistently return the same integer, provided no information
* used in {@code equals} comparisons on the object is modified.
* This integer need not remain consistent from one execution of an
* application to another execution of the same application.
* <li>If two objects are equal according to the {@code equals(Object)}
* method, then calling the {@code hashCode} method on each of
* the two objects must produce the same integer result.
* <li>It is <em>not</em> required that if two objects are unequal
* according to the {@link java.lang.Object#equals(java.lang.Object)}
* method, then calling the {@code hashCode} method on each of the
* two objects must produce distinct integer results. However, the
* programmer should be aware that producing distinct integer results
* for unequal objects may improve the performance of hash tables.
* </ul>
* <p>
* As much as is reasonably practical, the hashCode method defined by
* class {@code Object} does return distinct integers for distinct
* objects. (This is typically implemented by converting the internal
* address of the object into an integer, but this implementation
* technique is not required by the
* Java&trade; programming language.)

1. 这方法是用来优化hash tables的,例如 HashMap。

2. 只要对象数据没有改变(equals方法中使用的数据),不论什么时候去获取hashCode,都应该也一样。但不是不允许hashCode变化。

3.两个对象如果equals判定相等, 必须有相同的hashCode

4. 当equals判定两个对象不相等时,并不要求hashCode一定不相等。但是要明白,不同对象(equals比较)有不同的hashCode可以提高 哈希表的性能。

5. Object类的hashCode方法返回的是由地址转换的整型。不同对象hashCode不同。 它是native方法,其他语言实现的。

hashCode()方法被定义是用来使得哈希表可以高效工作的。hashCode 为什么对哈希表很重要呢? 例如我们用的 HashMap,当我们调用get(Object  key)时,首先就要查找在容器中是否存在这个对象,一般来说如何查找,肯定是要逐个去比较是否相等的,那就要使用equals方法才正确。但如果数据很多,equals的工作效率就很慢。这个时候就要有一个代替的方案:hashCode。当我们调用哈希容器的get(Object obj)方法时,首先查找是否有相同哈希值的对象,如果有再用equals比较是否相等,相等则返回该哈希处的对象。否则当然都是null,hashCode()返回的是一个整型值,比较自然很快。

总结:

  • 重写equals,必须要重写hashCode。equals判定相等,hashCode必须一致。
  • equals是判定两对象是否相等的充要条件。
  • hashCode方法是判定两对象是否相等的必要非充分条件。

在重写hashCode时,最好使用null安全的Objects.hashCode方法。或者当需要组合多个散列值时,调用Objects.hash方法直接生成。我们重写Employee类的equals和hashCode方法。

public boolean equals(Object otherObject)
{
// a quick test to see if the objects are identical
if (this == otherObject) return true; // must return false if the explicit parameter is null
if (otherObject == null) return false; // if the classes don't match, they can't be equal
if (getClass() != otherObject.getClass()) return false; // now we know otherObject is a non-null Employee
Employee other = (Employee) otherObject; // test whether the fields have identical values
return Objects.equals(name, other.name) && salary == other.salary && Objects.equals(hireDay, other.hireDay);
}
public int hashCode(){
return 7 * Objects.hashCode(name) + 11 * new Double(salary).hashCode() + 13 * hireDay.hashCode();
} 或 public int hashCode()
{
return Objects.hash(name, salary, hireDay);
}

计算数组类型的hashCode 可以使用 Arrays.hashCode(type[] a)方法。

9. 对象包装器类: Integer、Long、Float、Double、Short、Byte、Character、Void、Boolean。都是不可变类型。自动装拆箱。

10. "..."表示接受任意数量的对象。例如  Object...  同等于Object[]。

11. 反射。

  • Class 类。
Class cl = object.getClass(); //Object的getClass方法返回一个Class类型的实例,一个Class对象表示一个特定类的属性。
String className = new Date().getClass().getName() //值为"java.util.Date", cl.getName()返回该类的名字,名字包含包名.

这样我们就可以从一个对象实例获得其Class对象和类名。同样我们也可以通过类名获得对应的Class对象并构建类实例。

String className = "java.util.Date";
Class cl = Class.forName(className); //forName方法只有在className字符串是类名或接口名时才能够执行,否侧会抛checked exception。
//获得Class对象的另一种方法。T.class。T为java的任意类型
Class cl1 = Date.class;
Class cl2 = int.class;
Class cl3 = Double[].class;
//通过Class对象的newInstance()方法创建类的实例。 Date now = (Date)cl.newInstance();
//newInstance方法调用默认的构造函数初始化对象。如果没有默认或者无参的构造函数将报错。

这样我们就可以通过字符串获得具体需要执行的类,例如将类名写在配置文件中,在需要更换类的时候就不必再更改代码了。

String classNameStr = readFromProperties;        //"ClassA"
Class c = Class.forName(classNameStr);
ClassAInterface factory = (ClassAInterface)c.newInstance();

当按类名构造类实例需要传入参数时,可以使用 Constructor类的newInstance()方法。

  • 分析类的能力

  java.lang.reflect包中有三个类,Field、Method、Constructor,分别用于描述类的属性、方法、和构造器。 Class和这三个类都有一个方法叫做getModifiers(),该方法返回一个整型值,表示类、数据、方法和构造器的修饰属性,例如 1 表示public,可以使用Modefier类分析返回的值。这三个类也都有getName方法返回名称。

Class类

getName()

getSuperclass()

getModifiers()

getFields()---返回public 数据,getDeclareFields ---返回所有数据-----Field类

getMethods()---返回public 方法, getDeclareMethods---返回所有方法---Method类

getConstructors()---返回public 构造器,getDeclareConstructors--返回所有构造器---Constructor类

newInstance()

static forName

  Field

getName()

getType()----返回类型,Class对象

getModifiers()

  Method

getName()

getReturnType()

getModifiers()

getParameterTypes()

  Constructor

getName()

getModifiers()

getParameterTypes()

  • 运行时使用反射分析对象

  通过反射机制查看对象的数据,例:

Employee marry = new Employee("Marry", 50000,2022, 8, 1);
Class c = marry.getClass();
Field f = c.getDeclaredField("name");
f.setAccessible(true); //Importance setAccessible方法 设置为true时,使得Field.get()方法可以访问private属性。 /**
AccessibleObject类的静态方法setAccessible()如下,可以设置Field[]为可读取的。
public static void setAccessible(AccessibleObject[] array, boolean flag)
*/ String name = (String)f.get(marry); //Field.get() 方法返回的类型为Object,String类可以,但是当访问salary属性时就不行了,
//基本类型不是Object的子类,这时可以使用Field.getDouble()方法。
  •  通过invoke调用任意方法

这种方法不推荐使用, Method m1 = Employee.class.getMethod("raiseSalary", double.class);  m1.invoke(10) 当有返回值时,invoke返回的都是Object对象。

java基础二、类与继承的更多相关文章

  1. 【代码笔记】Java基础:类的继承(构造器)

    在Java中,创建对象的格式为: 类名 对象名 = new 类名(): 如: 1 JFrame jf = new JFrame(); 一个对象被创建出来时,经常要先做一些事这个对象才能正常使用,也可以 ...

  2. Java基础 -- 复用类(组合和继承)

    复用类有两种实现方式. 在新的类中产生现有类的对象,由于新的类是由现有类的对象所组成,所以这种方法称之为组合. 采用继承实现. 一  组合语法 下面创建两个类WaterSource和Sprinkler ...

  3. java基础/一个类A继承了类B,那么A就叫做B的派生类或子类,B就叫基类或超类。

    类重复,pulic class demo1 和class demo1 重复 无主类, 在cmd中输入命令: SET CLASSPATH=. (等号后为英文点符号),即可设置解释的路径为当前路径. 再次 ...

  4. Java基础-StringBuffer类与StringBuilder类简介

    Java基础-StringBuffer类与StringBuilder类简介 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.StringBuffer类 在学习过String类之后 ...

  5. Java面试题总结之Java基础(二)

    Java面试题总结之Java基础(二) 1.写clone()方法时,通常都有一行代码,是什么? 答:super.clone(),他负责产生正确大小的空间,并逐位复制. 2.GC 是什么? 为什么要有G ...

  6. 《Java基础——线程类》

    Java基础--线程类       一.线程的创建之Thread类: 规则: 通过声明一个新类作为子类继承 Thread 类,并复写 run() 方法,就可以启动新线程并执行自己定义的 run()方法 ...

  7. 第二十九节:Java基础知识-类,多态,Object,数组和字符串

    前言 Java基础知识-类,多态,Object,数组和字符串,回顾,继承,类的多态性,多态,向上转型和向下转型,Object,数组,多维数组,字符串,字符串比较. 回顾 类的定义格式: [类的修饰符] ...

  8. java基础-BigDecimal类常用方法介绍

    java基础-BigDecimal类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.BigDecimal类概述 我们知道浮点数的计算结果是未知的.原因是计算机二进制 ...

  9. java基础-BigInteger类常用方法介绍

    java基础-BigInteger类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.BigInteger类概述 Java中long型为最大整数类型,对于超过long ...

  10. java基础-Arrays类常用方法介绍

    java基础-Arrays类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Array类的概念 此类包含用来操作数组(比如排序和搜索)的各种方法.需要注意,如果指定 ...

随机推荐

  1. NC20583 [SDOI2016]齿轮

    题目链接 题目 题目描述 现有一个传动系统,包含了N个组合齿轮和M个链条.每一个链条连接了两个组合齿轮u和v,并提供了一个传动比x : y. 即如果只考虑这两个组合齿轮,编号为u的齿轮转动x圈,编号为 ...

  2. @ConditionalOnMissingBean 如何实现覆盖第三方组件中的 Bean

    1. 自定义一个简单 spring-boot 组件 创建 olive-starter 项目 对应的 pom.xml文件如下 <project xmlns="http://maven.a ...

  3. 4-5 Spring Boot

    1. 关于Spring Boot Spring Boot是Spring官方的一个产品,其本质上是一个基于Maven的.以Spring框架作为基础的进阶框架,很好的支持了主流的其它框架,并默认完成了许多 ...

  4. Solution -「CF1373G」Pawns

    小清新线段树题(( 每个位置的边只能向靠右的三个方向走,最后要走到一条基准线上.即对于一个点 \((x, y)\),它最后应该落在 \((k, y + |k - x|)\). 士兵可以一个一个进行移动 ...

  5. Note -「模拟退火」

    随机化算法属于省选芝士体系 0x01 前置芝士 你只需要会 rand 就可以啦! 当然如果你想理解的更透彻也可以先看看 爬山算法 0x02 关于退火 退火是一种金属热处理工艺,指的是将金属缓慢加热到一 ...

  6. typescript中的esModuleInterop选项

    当没有加esModuleInterop时 库的代码: export const a = 1; export default function b() {} 生成代码 exports.__esModul ...

  7. MySQL sql优化(摘抄自文档)

    前言 有人反馈之前几篇文章过于理论缺少实际操作细节,这篇文章就多一些可操作性的内容吧. 注:这篇文章是以 MySQL 为背景,很多内容同时适用于其他关系型数据库,需要有一些索引知识为基础. 优化目标 ...

  8. Linux—搭建Apache(httpd)服务

    1.httpd简介? http是Apache超文本传输协议服务器的主程序.它是一个独立的后台进程,能够处理请求的子进程和线程. http常用用的两个版本是httpd-2.2和httpd-2.4 Cen ...

  9. while,do while,for循环语句

    循环语句 循环包含三大语句-----while语句 do while语句 for语句 循环三要素 初始值(初始的变量值) 迭代量(基于初始值的改变) 条件(基于初始值的判断) while语句 var ...

  10. 学习javascript知识

    开始学习了 努力----努力----努力 从今天开始 绝不 三天打鱼两天晒网 先把基础再巩固一下