Java的三大特征之一——多态及接口相关内容

1.final关键字

①final可以修饰成员变量

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

②final可以修饰局部变量

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

③final可以修饰成员方法

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

④final可以修饰类

final修饰的类不能被继承

⑤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);//6d06d69c
		//The final field Person.name cannot be assigned
		//person.name  = "二狗";
	}
}	

2.接口

语法格式:

interface 接口名{

        属性;

        方法;

}

接口详解:

        ①使用关键字interface来声明接口

        ②接口中可以有属性,只不过属性必须赋值,因为默认带了public staticfinal,表示的是常量

        ③接口中的方法都是抽象方法,尽管没有带abstract,默认也是public的

        ④接口中一般都是抽象方法,也有特殊情况,在jdk1.8及以后的版本中增加了可以使用default修饰的方法,可以带有方法体

        ⑤接口中没有构造方法,也就意味着无法被实例化

        ⑥用一个普通类实现接口

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

        ⑧一个类可以实现多个接口

        ⑨一个接口可以继承另一个接口

案例:

电脑类: 去实现三个接口

1.电源接口

2.USB接口

3.网络接口

interface Adapter {//电源接口
	void chongDian ();//充电的方法
}
interface Type_C {//Type_c接口
	void mouse();//连接鼠标
}
interface Net {
	void internate();//连接网线
}
class Computers implements Adapter, Type_C, Net{

	@Override
	public void internate() {
		// TODO Auto-generated method stub
		System.out.println("电脑可以联网");
	}

	@Override
	public void mouse() {
		// TODO Auto-generated method stub
		System.out.println("电脑可以链接鼠标");
	}

	@Override
	public void chongDian() {
		// TODO Auto-generated method stub
		System.out.println("电脑可以充电");
	}
	
}
public class Demo3 {
	public static void main(String[] args) {
		Computers computers = new Computers();
		computers.chongDian();
		computers.mouse();
		computers.internate();
	}
}

3.多态

3.1多态的概念

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

3.2方法的多态

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

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) {
		
	}
}

3.3对象的多态

①必须有继承关系

②必修重写父类的方法

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

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();
		
	}
}

3.4多态的转型

3.4.1多态的向上转型

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

语法格式:父类  父类的引用=new 子类();

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

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();
		
	}
}

3.4.2多态的向下转型

语法格式:

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

子类 子类的引用=(子类)父类的引用;(强制类型转换)

想用向下转型的话,必须先向上转型

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);
	}
}

3.5instanceof关键字

在Java中instanceof是一个二元运算符,结果为true或false

语法格式:A instanceof B  (对象的引用  instanceof   类) 

目的:测试左边的对象是否是右边类的实力

注意:A是对象,B是类

通俗来说:左边的辈分只要比右边小或相等就返回true

class Animal {
	
}
class Dog extends Animal {
	
}
public class Demo1 {
	public static void main(String[] args) {
		
		//instanceof  语法格式   A  instanceof B
		//左边放的是对象   右边放到是类
		//判断左边的对象是否是右边的类的实例(子类也算是实例)
		//左边辈分小或者平辈  右边的类 才是true
		Animal animal = new Animal();
		
		System.out.println(animal instanceof Animal);//true
		Dog dog = new Dog();
		System.out.println(dog instanceof Animal);//true
		
		Animal animal2 = new Dog();
		System.out.println(animal2 instanceof Animal);//true
		
		
		System.out.println(animal instanceof Dog);//false
		System.out.println(animal2 instanceof Dog);//true
		
	}
}

4.static关键字

①static修饰成员变量

②static修饰成员方法

③static修饰代码块

4.1static修饰成员变量

语法格式:static  数据类型  变量名

使用:类.静态变量=值;

注意:

        ①使用static修饰的成员变量叫静态变量

        ②代码中对象还没有创建的时候,类已经加载了,这时,静态的属性已经存在了。早于对象的创建

        ③内存分析↓

 4.2静态方法

语法格式:

public static  返回值  方法的名字 (参数列表) {

}

调用静态方法: 类名.方法的名字()

class Student {
	String name;
	//只能通过对象来调用
	public   void eat() {
		System.out.println("吃水果");
	}
	//类名.方法的名字
	//静态方法的方法的中能不能使用 非静态的成员变量和方法?  不可以
	//Cannot make a static reference to the non-static field name
	public static void drink () {
		 
//		name = "xixi";
//		eat();
		 System.out.println(a);
		System.out.println("喝白开水");
	}
}
public class Demo2 {
	public static void main(String[] args) {
		Demo2.test();
		//Student.eat();
		Student.drink();
	}
	public static void test () {
		System.out.println("测试");
	}
}

4.3静态代码块

语法格式:

static {
    语句体
}

只要类加载了,这个静态代码块就会被执行

执行顺序:静态代码块->构造代码块->构造方法

class Teacher {
	
	
	{
		System.out.println("构造代码块");
	}
	public Teacher () {
		System.out.println("无参构造方法");
	}
	//只要类被加载了 就执行了
	static {
		System.out.println("执行了静态代码块");
	}
}
public class Demo3 {
	public static void main(String[] args) throws ClassNotFoundException {
		//Teacher teacher = new Teacher();
		Class.forName("com.qf.a_static.Teacher");//没有实例化对象,但是加载了 Teacher这个类的
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值