java小白零基础(六)

1.final关键字

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

2.接口【重点】

2.1生活中的接口

比如咱们的电脑留了很多的接口

类 关于字符串类 实现一个接口 序列化接口

2.2Java中的接口

语法格式:

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();
	}
}
2.3接口的详解
2.31语法格式:
语法格式:

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

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

3.接口下面 的方法都是抽象方法,尽管没有带abstract    默认也是public4.接口下面是一般都是抽象方法 有特殊的情况呢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();
	}
}
2.4案例

电脑类: 去实现三个接口

1.电源接口

2.USB接口

3.网络接口

package com.qf.c_interface;

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

2.多态

2.1多态概念

Java面向对象的三大特性: 封装 继承 多态

2.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) {
		
	}
}
2.3对象的多态

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

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

2.5多态的转型【重点】

2.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();
		
	}
}
2.5.2多态的向下转型

语法格式

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

举例

package com.qf.d_duotai;


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 Demo2 {
	public static void main(String[] args) {
		//演示语法格式
		Animal animal = new Dog();//父类  父类的引用 = new   子类();  向上转型
		//向下转型   子类  子类的引用 = (子类) 父类的引用;
//		Dog dog1 = (Dog)animal;//强转  在强转的时候一定先向上转 然后再向下转才不会报错的
//		dog1.eat();
		
		/**
		 * ClassCastException(类转换异常): com.qf.d_duotai.Animal cannot be cast to com.qf.d_duotai.Dog
	at com.qf.d_duotai.Demo2.main(Demo2.java:32)
		 */
		这个是错误的代码
//		Animal animal2 = new Animal();
//		Dog dog2 = (Dog) animal2;
//		
		//Exception in thread "main" java.lang.ClassCastException: com.qf.d_duotai.Dog cannot be cast to com.qf.d_duotai.Cat
		//at com.qf.d_duotai.Demo2.main(Demo2.java:38)

//		Cat cat1 = (Cat)animal;
//		Dog dog1 = new Dog();
		
		
	}
}

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

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

2.6instanceof关键字

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

A instanceof B

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

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

语法格式:

对象的引用  instanceof

返回值是true或者false

Animal  父类    Dog 子类
Dog dog = new Dog();
dog instanceof  Animal   true

Animal animal = new Animal();
animal instanceof Animal  true
Animal animal1 = new Dog();
animal1  instanceof Animal  
package com.qf.f_instaceof;

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
		
	}
}
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);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值