面向对象下

10.继承【重点】

继承至少得两个类:

语法格式:

class A {
	属性
	方法
} 
class B  extends A {
	B就可以继承你的属性和方法
}

继承其实就是将父类的属性和方法,子类中可以使用

1.成员变量(属性)

公开的(public)和默认的(啥也不写)属性,子类是可以使用

私有的属性,子类是无法使用

2.成员方法(方法)

公开的(public)和默认的(啥也不写)方法,子类是可以使用的

私有的方法,子类是无法使用

3.构造方法

new Son1(); 尽管你是 new 的儿子类 但是会执行父类的构造方法

1.先执行父类的构造方法,然后再执行子类的构造方法

2.如果父类中没有无参构造方法,子类也不能有无参构造方法

package com.qf.test;


class Father1 {
	//公开的和默认的属性可以被子类继承的
	public String name;//公开的属性
	int age;//默认的属性
	
	private int id;//私有化的
	
	public Father1 () {
		
	}
	
	public Father1(String name, int age, int id) {
		
		this.name = name;
		this.age = age;
		this.id = id;
	}
	//公开的方法
	public  void  eat () {
		System.out.println("吃红薯面");
	}
	//默认的方法
	void  work () {
		
		System.out.println("翻地球");
	}
	//私有话的方法  子类无法使用的e
	private void smoking () {
		System.out.println("抽旱烟");
	}
}
class Son1 extends Father1{

	public Son1 () {
		
	}
	public Son1(String name, int age, int id) {
		super(name, age, id);//调用父类的有参构造方法
		// TODO Auto-generated constructor stub
	}
	
	//mplicit super constructor Father1() is undefined. Must explicitly invoke another constructor
	//父类中不存在,儿子的无参构造方法也不能存在
//	public Son1 () {
//		System.out.println("这个是子类的无参构造方法");
//	}
	
	  //总结:   子类的构造方法必须依靠父类的构造方法的形似来进行创建
	
}
public class Demo10 {
	public static void main(String[] args) {
		Son1 son1 = new Son1();
		son1.name = "小灰灰";//发现确实可以继承
		son1.age = 17;//发现 默认的属性也是可以继承
		//the field Father1.id is not visible  不可见 
		//son1.id = 12;
		son1.eat();
		son1.work();
		//son1.smoking();
		
		
		
		
		
	}
}

10.1继承的概念

  1. B继承了A B叫A的子类 A叫B的父类 超类 基类 祖宗类
  2. Java中是单继承 (只能有一个父类) 但是可以多重继承

10.2关于父类和子类的内存分析

class Father {
 	String name;
 	int age;
}
class Son extnds Father {
	char sex;
}
main {
	Son son = new Son();
	
}
无论是父类和子类都在堆中的  同一个内存空间中

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6xyWLyTV-1681549203104)(file://D:/%E5%8D%83%E5%B3%B0%E6%95%99%E8%82%B2%E8%BD%AF%E4%BB%B6/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%E8%A7%86%E9%A2%91%E4%BB%A3%E7%A0%81/code/codeday11/1.png?lastModify=1679106961)]

10.3重写和重载【重点】

10.3.1重写(override)

重写的目的: 和继承有关 重写的是方法 子类是可以继承父类的非私有化的方法的

但是有的时候父类的方法需求满足不了子类的需求了,这个时候在子类中需要重写父类的方法

总结:

重写的规则:

1.必须有继承关系
2.在子类中去重写父类方法

3.父类的方法必须是公开的或者默认的方法
4.在子类中重写父类的方法除了方法体不太一样,其他都一样(方法的返回值, 方法的名字 ,方法的参数)

package com.qf.test;

class Father3 {
	public void eat () {
		System.out.println("吃窝窝头");
	}
}
class Son3 extends Father3{
	/*
	 * //重写:  就是把父类的方法重新写一遍,就是内容不一样
	 * 父类的方法不能动,子类的方法重新写了一遍
	 * 除了方法体中的内容不一样,其他都是一样的
	 * 
	 * 其他是啥:
	 * 	1.方法的名字
	 * 	2.方法返回值
	 * 	3.方法的参数
	 */
//	public void eat (String name) {
//		System.out.println(name + "吃大盘鸡");
//	 }
	@Override  //重写的严格限定 告知程序员 下面方法是重写的方法,不是自己独有的方法
	public void eat() {
		System.out.println("吃烤鸭");
	}
	
}
public class Demo5 {
	public static void main(String[] args) {
		Son3 son3 = new Son3();
		son3.eat();//调用的父类的方法
		//son3.eat("狗蛋");//调用的是子类独有的方法
	}
}

10.3.2重载(overlord)

在Java中,同一个类中,有很多的方法,如果方法的名字一样,参数列表不一样,那么方法之间叫重载

重载的规则:

1.方法的重载必须写在同一个类中
2.方法的名字必须一样
3.方法的参数列表一定不一样
4.方法的返回值可以一样也可以不一样
5.无参构造方法和有参构造方法也是方法的重载

package com.qf.test;

import java.beans.IntrospectionException;

class Person {
	
	public void eat () {
		System.out.println("吃饭");
	}
	public void eat (String name) {
		System.out.println(name + "吃黄焖酥肉");
	}
	public void eat (String kind, int a) {
		System.out.println(kind + "吃 "+a+"份黄焖酥肉");
	}
	public int eat (int a) {
		return a;
	}
}

public class Demo7 {
	public static void main(String[] args) {
		
	}
}

10.4super关键字

this:

​ 代表当前的对象

​ 1.this可以调用属性和方法

​ 2.this可以调用构造方法

super:

代表是父类的对象

1.super可以调用属性和方法 【不太重要】

2.super可以调用父类的构造方法

package com.qf.superTest;


class Father {
	String name;
	int age;
	
	public Father (String name, int age) {
		this.name = name;
		this.age = age;
	}
	
	public void eat () {
		System.out.println("吃饭");
	}
}
class Son extends Father{
	
	
	//无参构造方法
	public Son () {
		//JVM  虚拟机会默认调用调用父类的无参构造方法
		//super();//在子类的无参构造方法中去掉用了父类的无参构造方法
		//在子类的无参的构造方法中 去默认调用 父类的无参的构造方法 现在没有  就报错
		//
		
		super("狗蛋", 12);//调用父类的有参的构造方法
		//在一个构造方法中  能同时调用两个父类的构造方法吗? 不能的 就意味着  super()  这个默认的
		//无参构造方法就不再执行了
		
		//总之:  子类的构造方法 在调用父类的构造方法的时候,一定要确保  父类中有此对应好的构造方法
	}
	
	
	public void test () {
		super.name = "狗蛋";//调用父类的name变量    
		this.name = "二蛋";
		super.eat();//调用父类的eat方法
		
	}
}
public class Demo1 {
	public static void main(String[] args) {
		Son son = new Son();
		//son.eat();
		son.test();
	}
}

11.抽象类【重点】

在面向对象中,所有的对象都是可以通过类创建(描述的) 。反过来说并不是所有的类都能创建对象

如果一个类中没有足够的信息来描述一个对象的话,那么这个类叫抽象类

11.1抽象类声明

在Java中使用abstract修饰的类叫抽象类

在Java中使用abstract修饰的方法叫抽象方法

语法格式:

abstract class Person {
 
 //正常类的属性
 String name;
 //普通的成员方法
 public void eat () {
     System.out.print("吃饭");
 }
 //抽象方法   没有方法体的方法  没有大括号的
 public abstract void sleep ();
 
}
package com.qf.abstractTest;

abstract class Person {//抽象类
	//抽象类中可以有属性  方法  抽象方法
	
	public  void eat () {//普通方法
		System.out.println("吃饭啊");
	}
	//声明一个抽象方法  abstract  修饰符
	//是一个不完整的方法,  完整方法应该带方法体
	 public abstract void  test (int a);
	
}

//The type Man must implement(实现) the inherited abstract method Person.test()
//在普通类中 Man中,必须可以实例化的。所以Man所有的信息都要是完整的
//必须去重写 父类中 抽象的方法
class Man extends Person{
	@Override
	public void test(int a) {//是重写的 抽象的类的抽象方法
		System.out.println( a + "这个是子类重写的的抽象父类的抽象的方法");
		
	}
}
public class Demo1 {
	public static void main(String[] args) {
		//抽象类不能创建对象 咋办? 只能 被继承
//		Person person = new Person();
		Man man = new Man();
		man.eat();
		man.test();
	}
}

11.2抽象类的详解

1.如果一个类中有抽象方法,那么这个类一定是抽象类

2.抽象方法是一个没有方法体的方法,是一个不完整的方法

3.抽象类中能有普通方法吗? 可以

4.抽象类能被实例化吗? 不能

5.抽象类不能被实例化,然后可以使用普通类去继承抽象类

6.在子类中 必须去重写父类的所有的抽象方法,让抽象方法变得有意义

7.在抽象类中,非抽象的方法能不能在子类被重写的,可以的

8.一个抽象类可以继承另外一个抽象类

9.一个抽象类也可以继承一个非抽象的类

12.final关键字

final:

​ 字面意思: 最终的 最后的意思 修饰符

用法:

1.final可以修饰成员变量

final修饰的成员变量必须初始化(赋值),一旦被赋值之后,就无法被修改

2.final可以修饰局部变量

final修饰的局部变量可以暂时不用赋值, 使用的时候必须赋值,一旦赋值以后就不能改了

3.final可以修饰成员方法

final修饰的方法不能被子类重写

4.final可以修饰类

final修饰的类 不能被继承

5.final可以修饰对象的引用

这个引用一旦被赋值 就无法被修改

package com.qf.b_final;

 class Person {
	final String name = "狗蛋";
	
	public void test () {
		final int a;//可以修饰局部变量的
		a = 20;
		//a = 30; final 修饰的局部变量一旦被赋值   无法被修改 
		System.out.println(a);
	}
	//final修饰的成员方法 无法被
	public final void  eat () {
		System.out.println("嘻嘻  中午不想出去吃饭了");
	}
}
//class Man extends Person {
//	//Cannot override the final method from Person
	@Override
	public void  eat () {
		System.out.println("嘻嘻 hehe");
	}
//}
public class Demo1 {
	public static void main(String[] args) {
		 final Person person = new Person();//Person person  对象的引用
		 System.out.println(person);//15db9742
		Person person1 = new Person();
		System.out.println(person1);//6d06d69c
		
		
		//将person1赋值person
		//person = person1;
		System.out.println(person);//15db9742
		//The final field Person.name cannot be assigned
		//person.name  = "二狗";
	}
}	

13.接口【重点】

13.1Java中的接口

语法格式:

interface 接口的名字 {
 属性
 方法
}
package com.qf.c_interface;

interface USB {//接口的语法格式
	//Abstract methods do not specify a body   抽象的方法的不能有一个方法体
	public   void  connection();
	//接口下面的方法都是抽象的方法 是默认的抽象的方法
	
}
//接口无法被实例化 那咋办?   只能写一个普通类去实现(implements) 接口
class Computer implements USB{
	
	
	public void coding () {
		System.out.println("敲代码");
	}

	@Override
	public void connection() {
		System.out.println("连接u盘");
		
	}
}
public class Demo1 {
	public static void main(String[] args) {
		Computer computer = new Computer();
		computer.coding();
		computer.connection();
	}
}

13.2接口的详解

1.使用关键字 interface来声明Java中的接口

2.接口下面是可以有属性的, 只不过属性必须赋值,因为默认带了 public static final 表示的是常量

3.接口下面 的方法都是抽象方法,尽管没有带abstract 默认也是public的

4.接口下面是一般都是抽象方法 有特殊的情况呢jdk1.8之后,增加了可以使用default修饰的方法 是可以带有方法体的

5.接口下面没有构造方法,就意味无法实例化

6.用一个普通类去实现(implements)接口

7.实现接口的时候一定要重写所有的抽象的方法,默认的方法可以重写也可以不写

8.一个类可以实现(implements)多个接口 写法 implements D, C,F

9.一个接口可以去继承(extends)另外一个接口

package com.qf.c_interface;


interface C {
	void play();
}
//1.使用interface关键字来声明一个接口
interface A  extends C{
	//The blank final field name may not have been initialized
	//默认对属性 加了final修饰    默认加了static   默认也加了public
	public final static  String name = "狗蛋";//  使用 final static  修饰的量叫  常量(不可以改变的量)
	//接口下面可以常量  但是很 少
	int age = 20;
	
	
	void eat();//public   abstract 修饰的
	void sleep();
	
	default void test () {//和普通方法是一模一样的  如果不写 default 就报错  语法格式!!!
		System.out.println("测试默认方法");
	}
}
class B implements A{//一个类中可以实现多个接口

	@Override
	public void eat() {
		
		System.out.println("吃口香糖");
	}

	@Override
	public void sleep() {
		
		System.out.println("坐着睡");
	}

	@Override
	public void play() {
		// TODO Auto-generated method stub
		
	}

	
	
}
public class Demo2 {
	public static void main(String[] args) {
		//A  a= new A();不能实例化接口
		B b = new B();
		b.eat();
		b.sleep();
		b.test();
	}
}

14.多态

14.1多态的概念

多态的概念: 通俗的来说,就是多种形态, 在Java中去完成某一个行为,当不同的对象去完成的时候,会有不同的效果。

动物吃 狗吃 狗粮 猫吃猫粮

总的来说: 同一件事情,放在不同的对象上面会有不同的效果的

14.2方法的多态

方法的重写和重载就叫方法的多态

package com.qf.b_duotai;

class Person {
	public void  eat () {
		System.out.println("吃饭");
	}
	public void eat (String name) {
		System.out.println(name+"吃饭");
	}
}
class Man extends Person {
	//方法的重写
	@Override
	public void eat() {
		System.out.println("吃腰子");
	}
}
public class Demo1 {
	public static void main(String[] args) {
		
	}
}

14.3对象的多态

父类的引用指向子类的对象 或者 子类对象赋值给了父类的引用

Animal ani = new Dog();

1.必须有继承关系

2.必须重写父类的方法

3.父类的引用就可以调用子类重写方法,不能调用子类独有的方法

package com.qf.b_duotai;

class Animal {
	public void eat() {
		System.out.println("吃东西");
	}
	public void work () {
		System.out.println("工作");
	}
}
class Dog extends Animal{
	@Override
	public void eat() {
		System.out.println("吃狗粮");
	}
	public void sleep () {
		System.out.println("卧着睡!!!");
	}
}
public class Demo2 {
	public static void main(String[] args) {
		//父类的引用指向了子类对象  语法格式
		Animal animal = new Dog();
	
		//打印的结果是子类中重写的方法
		animal.eat();
		//父类的引用是调用不了子类独有的方法的
		//animal.sleep();
		animal.work();
		
	}
}

14.4多态的真实开发中应用

package com.qf.c_duotai;


class Person {
//	public void feed(Dog dog) {
//		dog.eat();
//	} 
//	public void feed(Cat cat) {
//		cat.eat();
//	}
	//方法的参数 是父类的引用   但是实际的参数  是 Dog  和 Cat  子类的真是的对象
	public void feed (Animal animal) {
		animal.eat();
	}
	//以后开发中 都是 方法的参数 是父类的引用,但是实际传参是子类的真实的对象
}
class Animal {
	public void eat() {
		System.out.println("吃饭");
	}
}
class Dog extends Animal{
	@Override
	public void eat() {
		System.out.println("吃大骨头");
	}
}
class Cat extends Animal{
	@Override
	public void eat() {
		System.out.println("吃鱼");
	}
}
public class Demo1 {
	public static void main(String[] args) {
		Person person = new Person();
		
		Animal animal1 = new Dog();
		
		person.feed(animal1);
		Animal animal2 = new Cat();
		person.feed(animal2);
		
		
	}
}

14.5多态的转型【重点】

14.5.1多态的向上转型

父类的引用指向了子类的对象

语法格式:

父类  父类的引用 = new  子类();

向上转型是自动转的将子类的对象赋值 父类的引用呢 小范围(子类)转为大范围(父类)的 是可以自动转的。

父类的引用可以调用父类的所有的方法,但是不能调用子类独有的方法。最终的调用结果看的是子类中重写的方法的

package com.qf.d_duotai;

class Person {
	public void eat() {
		System.out.println("吃饭");
	}
	
}
class Student extends Person {
	@Override
	public void eat() {
		System.out.println("吃有营养的东西");
	}
}
public class Demo1 {
	public static void main(String[] args) {
		Person person = new Student();//向上转型    父类的引用指向子类的对象
		person.eat();
		
	}
}

14.5.2多态的向下转型

父类想要调用子类独有的方法,需要向下转型

语法格式:

父类  父类的引用 = new  子类();
子类  子类的引用 = (子类) 父类的引用;   强制类型转换
package com.qf.e_duotai;

class Person {
	//父类 父类引用 = new 子类();
	//子类 子类的引用 = (子类) 父类的引用;
	public void test (Animal animal ) {
		//要求你必须给我打印的类型是Dog类型 咋办
		//父类的引用能执行子类独有的方法
//		animal.eat();
		//父类中不允许有eat方法,但是我想就想调用 Dog下面的eat方法  咋办?
	
		
		Dog dog = (Dog)animal;//向下转型
		dog.eat();
	
		
	}
}
class Animal {
	
}
class Dog extends Animal{
	public void eat () {
		System.out.println("吃大骨头");
	}
}
class Cat extends Animal{
	public void eat () {
		System.out.println("吃鱼");
	}
}

public class Demo2 {
	public static void main(String[] args) {
		Animal animal = new Dog();
		Person person = new Person();
		//test方法的参数是 一个Animal类型,  实参是子类
		person.test(animal);
		
		/**
		 * Exception in thread "main" java.lang.ClassCastException: com.qf.e_duotai.Cat cannot be cast to com.qf.e_duotai.Dog
	at com.qf.e_duotai.Person.test(Demo2.java:11)
	at com.qf.e_duotai.Demo2.main(Demo2.java:36)

		 */
		Animal animal2 = new Cat();
		person.test(animal2);
	}
}

15.instanceof关键字

在Java中 instanceof 是一个二元的运算符,和> >= < <= 类似的,他的结果是true或者false

A instanceof B

目的: 测试左边的对象 是否是右边类的实例。 注意 A 是对象 B 是类

左边的辈分 只要比 右边小或者相等 就返回值true

语法格式:

对象的引用  instanceof
package com.qf.e_duotai;

class Person {
	//父类 父类引用 = new 子类();
	//子类 子类的引用 = (子类) 父类的引用;
	public void test (Animal animal ) {
		//要求你必须给我打印的类型是Dog类型 咋办
		//父类的引用能执行子类独有的方法
//		animal.eat();
		//父类中不允许有eat方法,但是我想就想调用 Dog下面的eat方法  咋办?
		//Animal animal = new Dog();
		//animal instanceof Dog true 
		//Animal animal2 = new Cat();
		//animal instanceof Dog false
		if (animal instanceof Dog) {//
			Dog dog = (Dog)animal;//向下转型
			dog.eat();
		}
		if (animal instanceof Cat) {
			Cat cat = (Cat)animal;
			cat.eat();
		}
		
		/**
		 * 1.先向上转型
		 * 2.判断是否可以进行强壮
		 * 3.强转
		 */
	
		
	}
}
class Animal {
	
}
class Dog extends Animal{
	public void eat () {
		System.out.println("吃大骨头");
	}
}
class Cat extends Animal{
	public void eat () {
		System.out.println("吃鱼");
	}
}

public class Demo2 {
	public static void main(String[] args) {
		Animal animal = new Dog();
		Person person = new Person();
		//test方法的参数是 一个Animal类型,  实参是子类
		person.test(animal);
		
		/**
		 * Exception in thread "main" java.lang.ClassCastException: com.qf.e_duotai.Cat cannot be cast to com.qf.e_duotai.Dog
	at com.qf.e_duotai.Person.test(Demo2.java:11)
	at com.qf.e_duotai.Demo2.main(Demo2.java:36)

		 */
		Animal animal2 = new Cat();
		person.test(animal2);
	}
}

Dog) {//
Dog dog = (Dog)animal;//向下转型
dog.eat();
}
if (animal instanceof Cat) {
Cat cat = (Cat)animal;
cat.eat();
}

	/**
	 * 1.先向上转型
	 * 2.判断是否可以进行强壮
	 * 3.强转
	 */

	
}

}
class Animal {

}
class Dog extends Animal{
public void eat () {
System.out.println(“吃大骨头”);
}
}
class Cat extends Animal{
public void eat () {
System.out.println(“吃鱼”);
}
}

public class Demo2 {
public static void main(String[] args) {
Animal animal = new Dog();
Person person = new Person();
//test方法的参数是 一个Animal类型, 实参是子类
person.test(animal);

	/**
	 * Exception in thread "main" java.lang.ClassCastException: com.qf.e_duotai.Cat cannot be cast to com.qf.e_duotai.Dog
at com.qf.e_duotai.Person.test(Demo2.java:11)
at com.qf.e_duotai.Demo2.main(Demo2.java:36)

	 */
	Animal animal2 = new Cat();
	person.test(animal2);
}

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值