面向对象二

1.继承性

  • 多个类中有相同属性和行为时,可以将这些内容单独抽取到一个类中
  • 继承的出现减少了代码冗余,提高了代码的复用性
  • 继承的出现,有利于功能的拓展
  • 继承是多态的前提
  • 子类继承父类,就继承了父类的方法和属性
  • 子类既可以使用父类的方法和属性,也可以自己创建新的数据和方法
  • 子类不能直接访问父类中的私有成员变量和方法(可以通过父类中的public方法来间接访问)
  • Java只支持单继承
public class ExtendDemo {
    public static void main(String[] args) {
        Student student=new Student();
        student.age=12;
        student.name="mike";
       // student.secret="big secret";  
       // 父类私有变量  不能直接访问  
        student.setSecret("big secret");//通过
    }
    
}
class People{
    private String secret="secret";
    public String name;
    public String getSecret()
    {
        return this.secret;
    }
}
class Student extends People{
    int age;
    
}

2.方法的重写

  • 子类对父类的方法的重写,子类的方法会覆盖父类的方法
  • 子类重写的方法和父类的方法必须有相同的方法名称,参数列表
  • 子类重写的返回值不能大于父类方法的返回值
  • 子类重写方法的访问权限不能小于父类方法的访问权限
  • 子类方法抛出的异常不能大于父类被重写方法的异常
  • 子类不能重写父类private修饰的方法
  • 声明为static的方法改写不叫重写
package test;

import sun.net.www.content.text.plain;

public class ExtendDemo {
    public static void main(String[] args) {
         Person person=new Person();
         person.name="mike";
         person.age=12;
         System.out.println(person.getInfo());
         Student student=new Student();
         student.name="tom";
         student.age=13;
         student.school="SchoolOne";
         System.out.println(student.getInfo());//使用的是子类自己的getInfo
         Person stu=new Student();//父类引用子类对象
         stu.name="tom";
         stu.age=13;
         //stu.school="SchoolOne";  父类没有该属性
         System.out.println(stu.getInfo());
         //stu.say(); 父类没有该方法
    }
    
}
class Person{
    public String name;
    public int age;
    public String getInfo()
    {
        return name+"   "+age;
    }
}
class Student extends Person{
    public String school;
    public String getInfo()
    {
        return name+"   "+age+"   "+school;
    }
    public void say()
    {
        System.out.println("I am a stdudent");
    }
}

3.关键字super

  • super可以用于访问父类中定义的属性 子父类属性重名
  • super可以用于访问父类中定义的成员方法 子父类方法重名
  • super可以在子类构造器中调用父类的构造器
public class ExtendDemo {
    public static void main(String[] args) {
         Student student=new Student();
         student.say();//Father Child
    }
    
}
class Person{
    public void say()
    {
        System.out.println("Father");
    }
}
class Student extends Person{
    public void say()
    {
        super.say();
        System.out.println("Child");
    }
}
  • 子类所有的构造器都会默认访问父类中空参的构造器
  • 如果父类中没有空参构造器,子类的构造器必须指定自己的构造器或者指定父类的构造器,指定语句必须放在构造器首行
  • 如果父类中没有无参构造器,并且子类没有显示调用自己或父类的构造器,会编译出错
//父类有无参构造器
public class ExtendDemo {
    public static void main(String[] args) {
         Student student=new Student();
       
    }
    
}
class Person{
   
}
class Student extends Person{
    public Student()
    {
        System.out.println("子类无参构造器");
    }
    
}
//父类有无参构造器
public class ExtendDemo {
    public static void main(String[] args) {
         Student student=new Student();
       
    }
    
}
class Person{
   public Person()
   {
       
   }
}
class Student extends Person{
    public Student()
    {
        System.out.println("子类无参构造器");
    }
    
}
//父类没有无参构造器
public class ExtendDemo {
    public static void main(String[] args) {
         Student student=new Student();
       
    }
    
}
class Person{
    String name;
   public Person(String name)
   {
         System.out.println("父类带参构造器");
   }
  
}
class Student extends Person{
    public Student()
    {    
        super("tom");//必须加
        System.out.println("子类无参构造器");
    }
    
}
public class ExtendDemo {
    public static void main(String[] args) {
         Student student=new Student(1);
       
    }
    
}
class Person{
    String name;
   public Person(String name)
   {
         System.out.println("父类带参构造器");
   }
  
}
class Student extends Person{
    int age;
    public Student()
    {    
        super("tom");
        System.out.println("子类无参构造器");
    }
    public Student(int age)
    {
        this();
        //super("tom"); 也可以
        this.age=age;
    }
    
}

4.子类对象实例化过程

//逐级往上,先调用父类的构造器实例化父类
package test;



public class ExtendDemo {
    public static void main(String[] args) {
       new Wolf();
       
    }
    
}
class Creature{
    public Creature()
    {
        System.out.println("Creatuer 无参数的构造器");
    }
}
class Animal extends Creature{
    public Animal(String name)
    {
        System.out.println("Animal带一个参数的构造器,动物名字:"+name);
    }
    public Animal(String name,int age)
    {
        this(name);
        System.out.println("Animal带两个参数的构造器,age:"+age);
    }
}
class Wolf extends Animal{
   public Wolf()
   {
       super("灰太狼",3);
       System.out.println("Wolf无参的构造器");
   }
}
Creatuer 无参数的构造器
Animal带一个参数的构造器,动物名字:灰太狼
Animal带两个参数的构造器,age:3
Wolf无参的构造器

5. 多态性

  • 体现:父类的引用指向子类的对象
  • 编译时类型:由声明该变量时使用的类型决定
  • 运行时类型:由运行时实际赋给该变量的对象决定
  • 编译时:看左边 运行时:看右边
  • 如果父类的引用指向子类的对象,该引用变量就不能再访问子类中新加的属性和方法(父类中没有)
  • 注意:当父类和子类中有相同的属性和方法时,父类的引用指向子类的对象,引用访问的方法是子类重写的方法,但是成员变量却是父类引用自己的
public class ExtendDemo {
    public static void main(String[] args) {
      People stu=new Student();
      System.out.println(stu.name);
      stu.say();
      //stu.specialOfStudent();
      stu.specialOfPeiole();
       
    }
    
}
class People{
    String name="people";
    public void say()
    {
        System.out.println("I am a people");
    }
    public void specialOfPeiole()
    {
        System.out.println("specialOfPeople");
    }
}
class Student extends People{
    String name="student";
    public void say()
    {
        System.out.println("I am a student");
    }
    public void specialOfStudent()
    {
        System.out.println("specialOfStudent");
    }

}
people
I am a student
specialOfPeople

6、 ==和equals的区别

  • ==既可以比较基本类型,也可以比较引用类型。比较基本类型时,比较的时数值,比较引用类型时比较的时内存地址
  • equals时Object的方法。如果没有被重写过比较的也是==,但是像String的equals方法是被重写过的,比较的是内容

7、包装类的使用

基本数据类型包装类
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
booleanBoolean
charCharacter
//装箱  基本数据类型--->包装类
         //方式一  通过构造器
         Integer x=new Integer(5);
         //方式二  通过字符串参数
         Float f1=Float.parseFloat("12");
         //方式三  自动装箱
         Integer x1=12;


         //拆箱  包装类--->基本数据类型
         //方式一  调用包装类的xxxValue()方法
         Integer x2=new Integer(13);
         int x3=x2.intValue();
         Float f2=new Float(12.1);
         float f3=f2.floatValue();

         //方式二 自动拆箱
         int x4=x2;
         float f4=f2;


         //基本数据类型--->String类
         String s=String.valueOf(12);
         String s1=String.valueOf(12.1);

         //String类--->基本数据类型
         //方式一  使用相应包装类的parseXxx(String)方法
         int a=Integer.parseInt("12");
         float b=Float.parseFloat("12.1");
         
        //方式二  通过包装类的构造器
        int a1=new Integer("12");
        System.out.println(a1);


        //包装类--->String类
        //使用包装类对象的toString方法
        Integer c=new Integer(4);
        String cstr=c.toString();


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodePanda@GPF

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值