01

    科技2022-07-16  117

    1 OOP特征2:继承性

    当多个类中存在相同的属性和方法时,将这些内容抽取到单独的类中,其他的类只需要继承这个类就能具有相同的属性和方法。

    子类(派生类)-父类(基类);可以理解为“子类 is a 父类”

    类继承的语法是:class Subclass extends SuperClass()

    子类继承了父类,就继承了父类的方法和属性。在子类中,可以适用父类的属性和方法,也可以拥有自己的属性和方法。

    另外就是,对于private的父类私有属性和方法,子类不能直接访问。(可以通过get set方法)

    java中只支持单继承和多层继承

    public class Person { String name; char sex; int age; public Person(String name, char sex, int age) { this.name = name; this.sex = sex; this.age = age; } //因为都继承于Object所以再有toString方法就是子类重写了父类方法。 public String toString(){ return "姓名:"+name+", 性别:"+sex+", 年龄:"+age; } } public class Student extends Person { long number; int math; int english; int computer; public Student(String name, char sex, int age, long number, int math, int english, int computer) { super(name, sex, age); this.number = number; this.math = math; this.english = english; this.computer = computer; } public double aver(){ return (math+english+computer)/number; } public int max(){ int maxNumber = math; if (english > maxNumber){ maxNumber = english; }else if(computer > maxNumber){ maxNumber = computer; } return maxNumber; } }

    2 方法重写

    ①定义:

    在子类中可以根据需要对从父类继承来的方法进行改造,也成为方法的重置、覆盖。在程序执行时,子类覆盖父类的方法。

    ②要求:

    子类重写父类方法的时候必须具备和父类一样的方法名和参数列表。

    一般!就适用快捷键(Ctrl + O)或者Alt + insert —>插入override method

    子类重写的返回值类型不能大于父类被重写的方法的返回值类型。

    一般就是一样就好了

    子类重写方法的适用权限不能小于父类被重写的方法的访问权限

    一般就一样好了;子类不能重写父类声明为private权限的方法。

    子类方法抛出的异常不能大于父类被重写的异常

    ③注意:

    子类与父类中同名同参数的方法必须同时声明为非static的(即为重写),或者同时声明为static的(不是重写)。因为static方法是属于类的,子类无法覆盖父类的方法。

    ④实例:

    Person类:父类

    public class Person { public String name; public int age; public String getInfo(){ return "Name :"+ name + "\n" + "age : "+age; } }

    Student类:子类

    public class Student extends Person { public String school;//子类私有的属性 public String getInfo(){//重写了父类的getInfo方法。 return "Name: "+name+"\nage: "+age+"\nschool: "+school; } }

    测试类:

    public class TestOverride { @Test public void testStudent(){ Student student = new Student(); student.school = "哈尔滨师范大学"; student.age = 23; student.name = "纪伟"; System.out.println(student.getInfo()); } @Test public void testPerson(){ Person person = new Person(); person.age = 22; person.name = "孙妍"; String info = person.getInfo(); System.out.println(info); } }

    ⑤重写与重载

    重载,是指允许存在多个同名方法,但是这些方法的参数不同。不表现为多态性。

    重载,在方法调用之前就确定了要调用的方法,称为“早绑定”或“静态绑定”。

    重写,子类对于父类方法功能的改造或覆盖,方法名和参数列表都相同。表现为多态性。

    重写,由于多态,只有方法调用的那一刻,才确定要调用的方法,称为“晚绑定”或“动态绑定”

    3 四种访问权限修饰符

    4 关键字:super

    ①概述

    可以使用super来调用父类的指定操作

    super可以用于访问父类定义的属性 ”super.属性“

    super可以调用父类中定义的成员方法 ”super.方法“

    super可以在子类构造器中调用父类的构造器

    ②注意:

    super的追溯不仅限于直接父类

    调用父类的构造器!!!

    子类的所有构造器都默认访问父类中空参数的构造器当父类中没有空参的构造器时,子类必须通过**this(参数列表)或者super(参数列表)**语句指定调用本类或者父类中相应的构造器。同时,只能”二选一”,且必须放在构造器的首行。

    在实例化子类对象的时候,子类的构造器中首行都默认含有一个super(),默认调用父类的空参构造器,目的是调用父类非私有的属性和方法;

    也可以显示的调用super(xxx),调用父类有参构造,前提是父类具有这个有参构造。

    在实例化子类对象的时候,在堆内存中有一个子类的空间,这个空间里面有一个父集空间,存放的是父类的非私有的属性和方法。

    父类:

    public class Person { private String name; private int age; private Date birthDate; //三个参数的构造器 public Person(String name, int age, Date birthDate) { this.name = name; this.age = age; this.birthDate = birthDate; } //两个参数的构造器 public Person(String name, int age) { this.name = name; this.age = age; } //两个参数的构造器 public Person(String name, Date birthDate) { this.name = name; this.birthDate = birthDate; } //一个参数的构造器 public Person(String name) { this.name = name; } }

    子类:

    public class Student extends Person { private String school; public Student(String name, int age, String school) { super(name, age); this.school = school; } public Student(String name, String school){ super(name); this.school = school; } public Student(String school){ super();//编译出错!!!这是因为父类中没有空参的构造器,而此时系统将会调用父类无参的构造器。解决方法: 可以在父类中创建空参的构造器 this.school = school; } }

    5 子类对象的实例化过程

    从结果上看:(继承性) 子类继承父类后,就获取了父类中声明的方法和属性创建子类的对象,在堆空间,就会加载所有父类中声明的属性!! 从过程上看: 当我们通过子类的构造器创建子类对象时,我们一定会直接或间接的调用其父类的构造器,进而调用父类的父类的构造器…直到调用了java.lang.Object类中空参的构造器为止。正式因为加载过所有的父类结构,所以才可以看到内存中有父类中的结构,子类对象才可以考虑进行调用。

    6 OOP特征3:多态性

    ①概念

    理解多态性:可以理解为一个事物的多种形态

    何为多态性:父类的引用指向子类的对象(或子类的对象赋给父类的引用)

    Person p = new man(); Object obj = new Date();

    ②多态的使用

    多态的使用:虚拟方法调用

    有了对象的多态性以后,我们在编译期,只能调用父类中声明的方法,但在运行期,我们实际执行的是子类重写父类的方法

    总结:编译,看左边;运行,看右边

    ​ 多态是运行时行为!!!

    多态的使用前提:①类的继承关系;②方法的重写

    对象的多态性,只适用于方法,不适用于属性(编译和运行都看左边!)

    public class PersonTest { public static void main(String[] args) { //多态性:父类的引用指向了子类的对象 Person person = new Student("纪伟", 24, "哈尔滨师范大学"); person.say();//学生说话! Person person1 = new Person(); person1.say();//人说话 Student student = new Student("孙妍", 22, "青岛农业大学"); student.say();//学生说话! } }

    ③instanceof操作符

    x instanceof A:检验x是否为类A 的对象,返回值为boolean型。

    要求x所属的类与类A必须时子类和父类的关系,否则编译错误要求x属于类A的子类B,x instanceof A值也为true Person p1 = new Student; System.out.println(p1 instanceof Person);//true System.out.println(p1 instanceof Student);//true

    ④对象类型转换

    基本数据类型转换

    自动类型转换:小的数据类型 转换成 大的数据类型:比如说

    int a = 10; double b = a;

    或者:double c = 10;因为10是int比double小,所以可以自动转换成double类型的10.0

    强制类型转换:把大的数据类型强制转换成小的数据类型:比如说

    int a = (int) 3.1415926;

    输出为3,原因是强转 会导致精度降低。

    对象类型转换

    子类到父类的类型转换可以自动进行:父类到子类的类型转换必须通过造型(强制类型转换)实现无继承关系的引用类型间的转换是非法的在造型前可以使用instanceof操作符测试一个对象的类型

    ⑤代码实例

    测试类:

    public class GeometricTest { public static void main(String[] args) { //因为要调用测试类定义的方法,所以实例化测试类 GeometricTest test = new GeometricTest(); //实例化circle对象,赋初始值。 Circle c1 = new Circle(3.3, "white", 1.0); test.displayGeometricObject(c1); Circle c2 = new Circle(3.3, "white", 1.0); //调用test对象的比较面积方法: boolean isEquals = test.equalsArea(c1, c2); System.out.println("c1和c2的面积是否相等:"+isEquals); //实例化rectangle对象,赋初始值。 MyRectangle rectangle = new MyRectangle("white", 1.0, 2.1, 3.4); test.displayGeometricObject(rectangle); //实参是一个子类类型的,形参是父类类型的,调用重写的方法实际上就是调用了子类的重写方法。 } //显示对象的面积,形参是一个几何图形的父类,实参是一个具体图行的子类,都是调用重写的方法,这就用到了对象的多态性。 public void displayGeometricObject(GeometricObject o){ System.out.println("面积为:"+o.findArea()); } //判断两个面积是否相等 public boolean equalsArea(GeometricObject o1, GeometricObject o2){ return o1.findArea() == o2.findArea(); } }

    子父类:

    //父类 public class GeometricObject { protected String color;//父类的属性 protected double weight; protected GeometricObject(String color, double weight){//父类一个全参的构造器 this.color = color; this.weight = weight; } //父类的findArea方法 public double findArea(){ return 0.0; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } } //子类Circle类 public class Circle extends GeometricObject{ private double radius;//子类私有的半径属性 public Circle(double radius, String color, double weight) {//子类的构造器 super(color, weight);//必须显示用super调用父类的构造器 this.radius = radius; } @Override//重写父类的findArea方法 public double findArea() { return Math.PI*radius*radius; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } } //子类MyRectangle类 public class MyRectangle extends GeometricObject{ private double width;//私有的属性 private double height; public MyRectangle(String color, double weight, double width, double height) {//子类构造器 super(color, weight); this.width = width; this.height = height; } @Override//重写父类的findArea方法 public double findArea() { return width*height; } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } }

    7 Object类的使用

    ①概念

    Object类是所有Java类的根父类

    如果在类的声明中未使用extends关键字指明其父类,则默认父类为java.lang.Object类

    Object类中只声明了一个空参的构造器

    ②==操作符和equals方法

    ==:

    基本类型比较:只有两个变量的值相等,则为true

    引用类型比较:看是否指向同一个对象,也就是地址值相同则相等。

    用“==”进行比较的时候两边的数据类型必须兼容,(可自动转换的基本数据类型除外)

    equals():

    所有类都继承了Object类,也就获得了equals()方法。

    Object类的equals()方法,源码就是用“==”比较。

    子类如String、File、Date及包装类(Wrapper Class)都重写了父类Object的equlas方法,所以他们的比较不一样。

    区别:

    ==既可以比较基本数据类型(比较值)也可以比较引用数据类型(内存地址)equals,是属于java.lang.Object类里面的方法,如果没有重写的话就是==,像string等类的equals都是重写过的。

    ③如何重写equals()

    class User{ String name; int age; //重写其equals()方法 public boolean equals(Object obj){ if(obj == this){ return true; } if(obj instanceof User){ User u = (User)obj; return this.age == u.age && this.name.equals(u.name); } return false; } }

    ④toString()方法

    toString()方法再Object类中定义,其返回值是String类型,返回类名和它的引用地址。如下:

    practice08.Circle@14ae5a5 practice08.Circle@7f31245a

    可以根据需要在用户自定义类型中重写toString()方法

    如String 类重写了oString()方法,返回字符串的值。

    s1=“hello”;

    System.out.println(s1);//相当于System.out.println(s1.toString());

    基本类型数据转换成String类型时,调用了对于包装类的toString()方法

    int a = 10;

    System.out.println(“a=”+a);

    public class CircleTest { public static void main(String[] args) { Circle circle1 = new Circle(2.3); Circle circle2 = new Circle("white",3.3, 2.0); System.out.println("颜色是否相等:"+circle1.getColor().equals(circle2.getColor())); System.out.println("半径是否相等: "+circle1.equals(circle2)); System.out.println("默认调用:"+circle1);//默认调用了重写的toString方法 System.out.println(circle2.toString());//和上面是一样的都是调用重写的toString方法! } } public class GeometricObject { protected String color; protected double weight; protected GeometricObject(){ color = "white"; weight = 1.0; } protected GeometricObject(String color, double weight){ this.color = color; this.weight = weight; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } } public class Circle extends GeometricObject{ private double radius; public Circle() { super(); radius = 1.0; } public Circle(double radius) { // super(); this.radius = radius; } public Circle(String color, double weight, double radius) { super(color, weight); this.radius = radius; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } //计算圆的面积 public double findArea(){ return Math.PI*radius*radius; } //比较两个圆的半径是否相等,若相等返回true @Override public boolean equals(Object obj) { if (this==obj){ return true; } if (obj instanceof Circle){ Circle c = (Circle)obj; return this.radius == c.radius; } return false; } //重写toString方法 @Override public String toString() { return "Circle [radius="+radius+"]"; } }

    8 单元测试方法

    Java中的JUnit单元测试

    创建Java类的要求: 此类是public的此类提供公共的无参的构造器‘ 创建单元测试方法: 方法权限是public,没有返回值,没有形参 单元测试方法上声明注解:@Test import org.junit.Test; public class JunitTest{ @Test public void Test(){ } }

    9 包装类的使用

    ①装箱-拆箱(基本数据类型<–>)

    基本数据类型包装成包装类的实例–装箱

    int i = 500; Integer t = new Integer(i); Float f = new Float(4.56);

    获得包装类对象中包装的基本类型变量

    调用包装类的.xxxValue()方法:

    boolean b = bObj.booleanValue();

    ②类型转换

    字符串转换成基本数据类型

    通过包装类的构造器实现:

    int i = new Integer(12);

    通过包装类的parseXxx(String s)静态方法:

    Float f = Float.parseFloat(12.1);

    基本数据类型转换成字符串

    调用字符串重载的valueOf()方法:

    String fstr = String.valueOf(2.34f);

    更直接的方式:

    String intStr = 5 + “”

    ③用法举例

    int i = 500; Integer t = new Integer(i); 装箱:包装类使得一个基本数据类型的数据变成了类。 有了类的特点,可以调用类中的方法。 String s = t.toString(); // s = “500“,t是类,有toString方法 String s1 = Integer.toString(314); // s1= “314“ 将数字转换成字符串。 String s2=4.56; double ds=Double.parseDouble(s2); //将字符串转换成数字  拆箱:将数字包装类中内容变为基本数据类型。 int j = t.intValue(); // j = 500,intValue取出包装类中的数据  包装类在实际开发中用的最多的在于字符串变为基本数据类型。 String str1 = "30" ; String str2 = "30.3" ; int x = Integer.parseInt(str1) ; // 将字符串变为int型 float f = Float.parseFloat(str2) ; // 将字符串变为int型
    Processed: 0.011, SQL: 8