封装继承多态

封装

get\set

package com.oop.LX01;
public class person {
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void say(){
System.out.println("hello");
}
}

继承

1.extends

(1)只能单继承(一个儿子只能有一个父亲,一个父亲可以有多个儿子)

(2)儿子可以继承父亲(除了private)的所有的方法和属性

(3)子类会默认调用父类的无参的构造方法

package com.oop.LX01;
public class student extends person{
}
package com.oop;
import com.oop.LX01.student;
public class Demo03 {
public static void main(String[] args) {
student stu=new student();
stu.say();//hello
}
}

2.super

(1)调用父类的构造方法(有参无参都可以),必须在子类构造方法的第一行

(2)必须只能用在子类的方法或者构造方法中

(3)super和this不能同时调用构造方法

(4)super 与 this 的比较

    a.调用的类不同              this:自身类的对象或方法    super:父类的对象或方法

    b.使用前提不同              this:任意条件都可               super:只能在继承条件下使用

    c.代表的构造方法不同   this():本类的构造方法          super():父类的构造方法

package com.oop.LX01;
public class person {
public person(){
System.out.println("person构造方法");
}
public void test(){
System.out.println("person");
}
}
package com.oop.LX01;
public class student extends person{
public student(){
super();//super()属于隐藏代码,可写可不写,默认调用的父类的无参构造方法,必须写在子类构造方法的第一行
System.out.println("student构造方法");//person构造方法 student构造方法
}
public void test() {
System.out.println("student");
}
public void test1(){
test();//student
this.test();//student
super.test();//person
}
}
package com.oop;
import com.oop.LX01.student;
public class Demo03 {
public static void main(String[] args) {
student stu=new student();
stu.test1();
}
}

 

 3.方法重写

(1)前提必须存在继承关系,只能实现子类重写父类的方法

(2)方法名和参数列表必须一致,不能是static静态方法,重写的是方法体

(3)修饰符,可以扩大但不可缩小:public>Protect>Default>private

(4)抛出的异常,可以缩小但不可扩大:ClassNotFoundException-->Exception(大)

package com.oop.LX01;
public class B{
public void test() {
System.out.println("B");
}
}
package com.oop.LX01;
public class A extends B {
@Override
public void test(){
System.out.println("A");
}
}
package com.oop;
import com.oop.LX01.A;
import com.oop.LX01.B;
public class Demo03 {
public static void main(String[] args) {
A a=new A();
a.test();
B b=new A();
b.test();
}
}

 多态

 定义一:多态就是同一方法可以根据发送者对象的不同而采用多种不同的行为方式;一个对象(子类)的实际类型是确定的,但可以指向对象(父类)的引用的类型有很多。

定义二:在同一个方法中,由于参数类型的不同而导致执行结果不同的现象就是多态。

多态的条件

(1)必须存在继承

(2)父类的引用指向子类的对象 ,如Person p1= new Student();

(3)子类需要重写父类方法

        以下几种不允许重写

        a.static方法,属于类,他不属于实例

        b.final常量

        c.private私有的

多态的注意事项

(1)必须存在继承,父类和子类必须要有联系  类型转换异常会有ClassCastException!

(2)多态是方法的多态,属性没有多态 

多态的作用

(1)父类的引用可以指向子类的对象

(2)把子类转换成父类,向上转换;把父类转换成子类,向下转换,需要强制转换,会丢失父类的方法

(3)方便方法的调用,减少重复的代码

例一:(定义一)

package com.oop.LX01;
public class B{
public void test() {
System.out.println("B");
}
}
package com.oop.LX01;
public class A extends B {
@Override
public void test(){
System.out.println("A");
}
public void test1(){
System.out.println("AAA");
}
}
package com.oop;
import com.oop.LX01.A;
import com.oop.LX01.B;
public class Demo03 {
public static void main(String[] args) {
A a1=new A();// A能调用的方法可以是自己类的也可以是父类的
B a2=new A();// B父类型,可以指向子类,但是不能调用子类独有的方法
Object a3=new A();
a1.test();//A
a2.test();//A 子类重写了父类的方法,执行子类的方法
a1.test1();//AAA a2.test1()不行
}
}

 例二:(定义二)

interface Animal{//定义一个接口
void shout();//定义抽象的shout()方法
}
class Cat implements Animal{//定义类实现接口
@Override
public void shout() {
System.out.println("喵喵");
}
}
class Dog implements Animal{//定义类实现接口
@Override
public void shout() {
System.out.println("汪汪");
}
}
public class DT {
public static void main(String[] args) {
Animal a1= new Cat();//创建Cat对象,使用Animal类型的变量a1引用
Animal a2= new Dog();//创建Dog对象,使用Animal类型的变量a2引用
animalshout(a1);//将a1作为参数传入
animalshout(a2);//将a2作为参数传入
}
public static void animalshout(Animal a){//定义静态的方法,接收一个类型为Animal的参数
a.shout();
}
}

 

 

 1.instanceof:可以判断一个对象是否为某个类(或接口)的实例或者子类实例

public static void main(String[] args) {
//继承关系 (1)Object>String(2)Object>Person>Teacher(3)Object>Person>Student
Object object =new Student();
System.out.println(object instanceof Student);//true
System.out.println(object instanceof Person);//true
System.out.println(object instanceof Object);//true
System.out.println(object instanceof Teacher);//false
System.out.println(object instanceof String);//false
Person person =new Student();
System.out.println(person instanceof Student);//true
System.out.println(person instanceof Person);//true
System.out.println(person instanceof Object);//true
System.out.println(person instanceof Teacher);//false
//System.out.println(person instanceof String);//编译报错
Student student =new Student();
System.out.println(student instanceof Student);//true
System.out.println(student instanceof Person);//true
System.out.println(student instanceof Object);//true
//System.out.println(student instanceof Teacher);//编译报错
//System.out.println(student instanceof String);//编译报错
Person person1 =new Person();
System.out.println(person1 instanceof Student);//false
}

2.子类父类转换

public class Person {
public void run(){
System.out.println("run");
}
}
public class Student extends Person{
public void go(){
System.out.println("go");
}
}
public class Test {
public static void main(String[] args) {
Person obj =new Student();
//obj.go();//obj调不到子类的go()方法,只能调用重写的父类的方法,不能调用子类独有的方法
Student stu=(Student) obj;//强制转换
stu.go();
//以上两行可以简写成((Student)obj).go();
}
}

 

posted @ 2022-03-22 23:08  初心不曾负  阅读(50)  评论(0)    收藏  举报