final keyword
The final keyword is the final meaning and can be modified (method, variable, class)
Features of final modification
Modification method: indicates that the method is the final method and cannot be overridden
Modified variable: indicates that the variable is a constant and cannot be assigned again
Modified class: indicates that the class is the final class and cannot be inherited
/ Variables are stored in the stack
/new things are stored in the heap
基本数据类型变量:其值不能被更改
//常量的命名规范:如果是一个单词,所有字母大写,如果是多个单词,所有字母大写
//但中间需要使用_分隔
final int NAX = 10;
final int MAX_VALUE = 20;
引用数据类型变量:内存地址不能被更改,但是可以修改对象的属性值
final Student stu = new Student();
stu.setName("张三");
stu.setName("李四");
//final修饰成员变量 初始化时机
//1.在创建的时候,直接给值
//2.在构造方法结束之前,完成赋值
Code block :
The code enclosed in {} is called a code block
Partial code block:
Location: defined in the method
Role: Limit the life cycle of variables, release them early, and improve memory utilization
Construct code block:
Location: outside the method in the class
Features: Every time the construction method is executed, the code block will be executed and executed before the construction method is executed
Function: Extract the same code in multiple construction methods into the construction code block to improve the reusability of the code
Static code block:
Location: outside the method in the class
Features: It needs to be modified by the static keyword, loaded with the loading of the class, and only executed once
Function: Do some data initialization operations when the class is loaded, generally used to install the driver
interface:
Realize more
When all methods in a class are abstract methods, we can define them as interfaces
Interface is also a reference data type, it is more abstract than abstract class
Two important meanings of the existence of interfaces
1. Definition of rules
2. Extension of the program
Definition and characteristics of the interface:
The interface is defined with the keyword interface
public Interface interface name{}
Interface cannot be instantiated
There is an implementation relationship between an interface and a class, which is represented by the implements keyword
public class class name implements interface name {}
Interface subclass (implementation class)
Either rewrite all abstract methods in the interface
Either an abstract class
example:
public class Demo1 {
public static void main(String[] args) {
Cat c = new Cat();
c.catchMouse();
Dog d = new Dog();
d.catchMouse();
}
}
interface CM{
public abstract void catchMouse();//抽象方法
}
class Cat implements CM{
//接口实现
@Override
public void catchMouse() {
//重写方法
System.out.println("猫抓老鼠");
}
}
class Dog implements CM{
//接口实现
@Override
public void catchMouse() {
//重写方法
System.out.println("狗拿耗子");
}
}
Note: The implementation relationship between interfaces and classes can be implemented individually or in multiple implementations.
public class class name implements interface 1, interface 2{}
public class Test1Interface {
/*
接口定义格式:
public interface 接口名{}
类实现接口的格式:
public class 类名 implements 接口名{}
*/
public static void main(String[] args) {
//Inter i = new Inter();接口不允许创建对象
InterImpl ii = new InterImpl();
ii.study();
}
}
public class InterImpl implements Inter,InterA{
@Override
public void study() {
System.out.println("我是实现类中的study方法");
}
@Override
public void print1() {
}
@Override
public void print2() {
}
}
public interface Inter {
public abstract void study();
}
public interface InterA {
public abstract void print1();
public abstract void print2();
}
Features of members in the interface:
Member variables:
接口中成员变量只能是常量
系统会默认加入三个关键字:public static final
Construction method:
Not in the interface
Member method:
Member method: only abstract method, the system will default to two keywords
public abstract
public class TestInterface {
/*
接口中成员变量只能是常量,系统会默认加入三个关键字
public static final
构造方法:接口中没有
成员方法:只能是抽象方法,系统会默认给两个关键字
public abstract
*/
public static void main(String[] args) {
System.out.println(Inter.NUM);
}
}
class InterImpl extends Object implements Inter{
//所有的类默认的父类是Object
public InterImpl(){
super();
}
public void method(){
//num = 20;
System.out.println(NUM);
}
@Override
public void show() {
}
}
interface Inter {
//接口中的变量是常量,默认会被final,static,public关键字修饰
public static final int NUM = 10;
//public Inter(){}
public abstract void show();
}
Features of interface members in JDK8 version:
After JDK8 version:
1. It is allowed to define non-abstract methods in the interface, but it needs to be modified with the keyword default, these methods are default methods
Role: Solve the problem of interface upgrade
2. Static static method is allowed to be defined in the interface
The definition format of the default method in the interface:
Format: public default return value type method name (parameter list) ()
Example: public default void show() ()
Notes on the default method in the interface:
The default method is not an abstract method, so it is not mandatory to be rewritten. But it can be rewritten, remove the default keyword when rewriting
public can be omitted, default cannot be omitted
If multiple interfaces are implemented and the same method declaration exists in multiple interfaces, the subclass must override the method
Notes on static methods in interfaces:
1. Static methods can only be called by interface name, not by implementation class name or object name
2.public can be omitted, static cannot be omitted
The relationship between classes and interfaces:
The relationship between class and class:
Inheritance relationship, only single inheritance, but multi-layer inheritance
The relationship between classes and interfaces:
The realization relationship can be implemented in a single or multiple implementations, and multiple interfaces can be implemented in colleagues who inherit a class
//If the parent class and interface have the same method declaration, the parent class
The relationship between interface and interface:
Inheritance relationship can be single inheritance or multiple inheritance
Polymorphism:
Pass parameters pass parent class
The parent class reference points to the child class object
The same object in different forms at different moments
Example: cat
We can say that a cat is a cat: cat cat = new cat();
We can also say that cats are animals: animal = new cat();
The cats here show different forms at different moments, this is polymorphism
The premise and manifestation of polymorphism:
Has inheritance/implementation relationship
There are ways to rewrite
Have a parent class reference to a subclass object
example:
public class Test1Polymorphic {
/*
多态的前提:
1.要有(继承\实现)关系
2.要有方法重写
3.要有父类引用,指向子类对象
*/
public static void main(String[] args) {
//当前事物是一只猫
Cat c = new Cat();
//当前事物是一只动物
Animal a = new Cat();
a.eat();
}
}
class Animal{
public void eat(){
System.out.println("动物吃饭");
}
}
class Cat extends Animal{
@Override
public void eat() {
System.out.println("猫吃鱼");
}
}
Features of member access in polymorphism:
Construction method: Like inheritance, the subclass will access the parent class construction method through super
Member variables: compile and look at the left (parent class), execute look at the left (parent class)
Member method: compile and look at the left (parent class), execute and look at the right (parent class)
public class Test2Polymorphic {
/*
多态成员访问特点:
成员变量:编译看左边(父类),执行看左边(父类)
成员方法:编译看左边(父类),执行看右边(父类)
*/
public static void main(String[] args) {
Fu f =new Zi();
System.out.println(f.num);//10,但必须有父类中的num
f.method();//Zi...method 父类中必须有method()方法
}
}
class Fu{
int num =10;
public void method(){
System.out.println("Fu...method");
}
}
class Zi extends Fu{
int num = 20;
public void method(){
System.out.println("Zi...method");
}
}
Why is the access of member variables and member methods different?
1. Because member methods are rewritten, but member variables do not
The advantages and disadvantages of polymorphism:
Benefit: Improve code scalability
Specific embodiment: When defining a method, using the parent type as a parameter, the method can accept any child object of this parent class
Disadvantages: Cannot call subclass-specific functions
example:
public class Test3Polymorphic {
public static void main(String[] args) {
useAnimal(new Dog());
useAnimal(new Cat());
}
public static void useAnimal(Animal a){
a.eat();
}
}
abstract class Animal{
public abstract void eat();
}
class Dog extends Animal{
@Override
public void eat() {
System.out.println("狗吃肉");
}
}
class Cat extends Animal{
@Override
public void eat() {
System.out.println("猫吃鱼");
}
}
Transformation in polymorphism:
Upward transformation: from son to father
References to parent child class object
Downward transformation: from parent to child
Parent class reference to subclass object
example:
public class Test4Polymorpic {
public static void main(String[] args) {
//1.向上转型:从子到父
// 父类引用指向子类对象
Fu f = new Zi();
f.show();
//多态的弊端 不能调用子类特有的功能
//f.method();
//A.直接创建子类对象
//B.向下转型
//2.向下转型:从父到子
// 父类引用转为子类对象 强制转换
Zi z = (Zi)f;
z.method();
}
}
class Fu{
public void show(){
System.out.println("Fu...show");
}
}
class Zi extends Fu {
@Override
public void show()
{
System.out.println("Zi...show");
}
public void method() {
System.out.println("我是子类特有的方法,method");
}
}
The risks of transformation in polymorphism
If the converted reference type variable, the corresponding actual type and the target type are not the same type, the name will appear ClassCastException when converting
Avoid forced transfer problems
Keyword: instanceof
Use format:
Variable name instanceof type
Popular understanding: Determine whether the variable on the left of the keyword is the type on the right, and return the result of the boolean type
example:
public class Test3Polymorphic {
public static void main(String[] args) {
useAnimal(new Dog());
useAnimal(new Cat());
}
public static void useAnimal(Animal a){
a.eat();
if(a instanceof Dog){
Dog dog = (Dog) a;
dog.watchHome();//ClassCastException类型转换异常
}
// Dog dog = (Dog) a;
// dog.watchHome();//ClassCastException类型转换异常
}
}
abstract class Animal{
public abstract void eat();
}
class Dog extends Animal{
@Override
public void eat() {
System.out.println("狗吃肉");
}
public void watchHome() {
System.out.println("看门");
}
}
class Cat extends Animal{
@Override
public void eat() {
System.out.println("猫吃鱼");
}
}