当前位置: 移动技术网 > IT编程>开发语言>Java > 荐 JAVA10——final关键字_封装_多态_抽象类

荐 JAVA10——final关键字_封装_多态_抽象类

2020年07月12日  | 移动技术网IT编程  | 我要评论

目录

final关键字

封装/隐藏(encapsulation)

多态(polymorphism)

内存分析

抽象类


final关键字

  • 修饰变量:
    常量
  • 修饰方法:
    该方法不可被子类重写。但是可以被重载
  • 修饰类:
    修饰的类不能有子类,不能被继承。比如:Math、 String

eg1:

public static void main(String[] args) {
	final int MAX_VALUE = 200;	
	MAX_VALUE = 100;
}

第二句报错:

The final local variable MAX_VALUE cannot be assigned. It must be blank and not using a compound assignment 

final修饰的常量不能再更改 

eg2:

public class Animal {

	public final void run(){
		System.out.println("跑");
	}
	
}


class Bird extends Animal{
	
	public void run(){	
		super.run();
		System.out.println("飞");
	}
	
}

子类run方法报错:

Multiple markers at this line
    - overrides demo0710.testFinal.Animal.run
    - Cannot override the final method from  

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

eg3:

public final class Animal {

	public void run(){
		System.out.println("跑");
	}
	
}


class Bird extends Animal{
	
	public void run(){	
		super.run();
		System.out.println("飞");
	}
	
}

第二个类报错:

The type Bird cannot subclass the final class Animal

 final修饰的类不能被继承

封装/隐藏(encapsulation)

  • 为什么需要封装?封装的作用和含义?
    隐藏对象内部的复杂性,只对外公开简单的接口。便于外界调用,从而提高系统的可扩展性、可维护性
  • 程序设计要追求“高内聚,低耦合”
    高内聚就是类的内部数据操作细节自己完成,不允许外部干涉
    低耦合:仅暴露少量的方法给外部使用

使用访问控制符,实现封装

封装要点

  同一个类 同一个包中 子类 所有类
private      
default(什么都不写)    
protected  
public
  • 类的属性的处理:
    1.一般使用 private.(除非本属性确定会让子类继承)
    2.提供相应的get/set方法来访问相关属性。这些方法通常是 public,从而提供对属性的读取操作。(boolean变量的get方法是用is开头)
  • 一些只用于本类的辅助性方法可以用 private
    希望其他类调用的方法用 public

eg private:

Test01.java

public class Test01 {
	private String str;
	private void print(){
                String s = str;
		System.out.println("Test01.print()");
	}
}

Test02.java

public class Test02 {
	public static void main(String[] args) {
		Test01 t = new Test01();
		t.print();
	}
}

Test01调用str正常,Test02调用print时报错:

The method print() from the type Test01 is not visible

eg default:

Test01.java

public class Test01 {
	private String str;
	void print(){
		String s = str;
		System.out.println("Test01.print()");
	}
}

Test02.java

public class Test02 {
	public static void main(String[] args) {
		Test01 t = new Test01();
		t.print();
	}
}

 都运行正常

Test03.java(在另一个文件夹)

public class Test03 {
	public static void main(String[] args) {
		Test01 t = new Test01();
		t.print();
	}
}

调用print时报错:

The method print() from the type Test01 is not visible

 eg protected:

Test01.java

public class Test01 {
	private String str;
	protected void print(){
		String s = str;
		System.out.println("Test01.print()");
	}
}

Test03.java(在另一个文件夹)

public class Test03 {
	public static void main(String[] args) {
		Test01 t = new Test01();
		t.print();
	}
}

Test04.java(在另一个文件夹)

public class Test04 extends Test01 {
	
	public void ttt(){
		super.print();
		print();
	}
	
	public static void main(String[] args) {
		Test04 t = new Test04();
		t.print();
	}
	
}

3会报错,4运行正常

提供相应的get/set方法来访问相关属性eg:

public class Man {
	private String name;
	private String id;
	public static int cc;
	public static final int MAX_SPEED = 120;
	
}

快速生成get/set方法:

右键——Source——Generate Getters and Setters

可根据需要添加

多态(polymorphism)

  • 多态性是OP中的一个重要特性,主要是用来实现动态联编的,程序的最终状态只有在执行过程中才被决定而非在编译期间就决定了。这对于大型系统来说能提高系统的灵活性和扩展性。
  • java中如何实现多态?使用多态的好处?
    引用变量的两种类型:
    • 编译时类型(模糊一点,一般是一个父类)
      由声明时的类型决定
    • 运行时类型(运行时,具体是哪个子类就是哪个子类)
      由实际对应的对象类型决定。
  • 多态的存在要有3个必要条件:
    要有继承,要有方法重写,父类引用指向子类对象

 eg:
定义类:

public class Animal {
	String str;
	public void voice(){
		System.out.println("普通动物叫声");
	}
}

class Cat extends Animal {
	public void voice(){
		System.out.println("喵喵喵");
	}
}

class Dog extends Animal {
	public void voice(){
		System.out.println("汪汪汪");
	}
}

class Pig extends Animal {
	public void voice(){
		System.out.println("哼哼哼");
	}
}

测试方法,简化前:

public class Test {
	
	public static void testAnimalVoice(Cat c){
		c.voice();
	}
	
	public static void testAnimalVoice(Dog c){
		c.voice();
	}
	
	public static void testAnimalVoice(Pig c){
		c.voice();
	}
	
	public static void main(String[] args) {
		Cat c = new Cat();
		testAnimalVoice(c);
	}
}

简化后:

public class Test {
	
	public static void testAnimalVoice(Animal c){
		c.voice();
	}
	
	
	/*public static void testAnimalVoice(Dog c){
		c.voice();
	}
	
	public static void testAnimalVoice(Pig c){
		c.voice();
	}*/
	
	public static void main(String[] args) {
		Cat c = new Cat();
		testAnimalVoice(c);
	}
}

内存分析

代码:

Animal.java

public class Animal {
	String str;
	public void voice(){
		System.out.println("普通动物叫声");
	}
}

class Cat extends Animal {
	public void voice(){
		System.out.println("喵喵喵");
	}
	public void catchMouse(){
		System.out.println("抓老鼠");
	}
}

Test.java

public class Test {
	
	public static void testAnimalVoice(Animal c){
		c.voice();
		if(c instanceof Cat){
			((Cat) c).catchMouse();
		}
	}
	
	public static void main(String[] args) {
		Animal a = new Cat();
		Cat a2 = (Cat)a;
		testAnimalVoice(a);
	}
}

抽象类

  • 为什么需要抽象类?如何定义抽象类?
    • 是一种模版模式。抽象类为所有子类提供了一个通用模版,子类可以在这个模版基础上进行扩展
    • 通过抽象类,可以避免子类设计的随意性。通过抽象类可以做到严格限制子类的设计,使子类之间更加通用
  • 要点:
    • 有抽象方法的类只能定义能抽象类抽象类不能实例化,及不能用new来实例化抽象类
    • 抽象类可以包含属性、方法、构造方法。但是构造方法不能用来new实例,只能用来被子类调用
    • 抽象类只能用来继承
    • 抽象方法必须被子类实现

eg:

public abstract class Animal {
	String str;
	public abstract void run();
	public void breath(){
		System.out.println("呼吸");
	}
}

class Cat extends Animal{

	@Override
	public void run() {
		System.out.println("猫步小跑");
	}
	
}

class Dog extends Animal{

	@Override
	public void run() {
		System.out.println("狗跑");
	}
	
}

有抽象方法它的类必须是抽象类,此例适用于需要全部重写并且每个子类都要定义的情况。定义抽象类和抽象方法后,该抽象类的子类必须重写抽象类中的抽象方法

 

本文地址:https://blog.csdn.net/qiao39gs/article/details/107266004

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网