[零基础学JAVA]Java SE面向对象部分-15.面向对象高级(03)
上季内容回顾:1、super与this关键词
2、方法的重载与覆写
本季首要知识点:
1、final关键词
2、简单理解一下笼统类与接口(是JAVA中最主要的部分)
final关键词
终结器——final
1、被final符号的类不能被承继
finalclassA
{
};
classBextendsA
{
};
验证一下:
2、被final符号的方法不能被子类覆写
finalclassA
{
publicfinalvoidprint(){}
};
classBextendsA
{
publicvoidprint(){}
};
3、被final符号的变量就变成常量,假如变成常量,则以后不能修改
classA
{
finalStringNAME=Hello;
publicfinalvoidprint()
{
NAME=World;
}
};
之前在声明变量的时候是第一个单词的首字母小写,以后每个单词的首字母大写。假如使用final声明常量,则一切单词的字母都要大写。
重点:大局常量:
·static:是一切目标共享的
·final:是一个常量
·public:表明可以让外部看见
publicstaticfinalStringFLAG=redking.blog.51cto;大局常量
笼统类
笼统类:包括一个笼统方法的类就称为笼统类。
笼统方法:只声明而未实现的方法称为笼统方法。
方法没有方法体(方法体:“{}”),则称为是一个笼统方法。
除了笼统方法之外别的的定义好像一般类相同。
笼统类=一般类的功能+笼统方法
abstractclassA
{
publicstaticfinalStringFLAG=redking.blog.51cto;
publicvoidprint()
{
//有方法体,所以是一个一般方法
System.out.println(HelloWorld~~~);
}
//此处定义了一个笼统方法
publicabstractvoidfun();
};
publicclassDemo04
{
publicstaticvoidmain(Stringargs)
{
Aa=newA();
}
};
假如要使用一个笼统类,不能直接实例化,笼统类是有必要有子类的。
笼统类有必要被承继,被承继的子类假如不是一个笼统类的话,则肯定要覆写全面的笼统方法。
abstractclassA
{
publicstaticfinalStringFLAG=redking.blog.51cto;
publicvoidprint()
{
//有方法体,所以是一个一般方法
System.out.println(HelloWorld~~~);
}
//此处定义了一个笼统方法
publicabstractvoidfun();
};
//子类中覆写了笼统类中的全面笼统方法
classBextendsA
{
publicvoidfun()
{
//super.FLAG也可以写成FLAG,由于FLAG现已是大局常量了哈~~~
System.out.println(FLAG=+super.FLAG);
}
};
publicclassDemo04
{
publicstaticvoidmain(Stringargs)
{
Bb=newB();
b.fun();
b.print();
}
};
验证效果,证明笼统类有必要这样写哈~~~
笼统类的定义
笼统类的使用规则
笼统类的考虑
abstractclassPerson
{
//Person类应该有名字和年纪
privateStringname;
privateintage;
publicPerson(Stringname,intage)
{
this.name=name;
this.age=age;
}
publicvoidsetName(Stringname)
{
this.name=name;
}
publicvoidsetAge(intage)
{
this.age=age;
}
publicStringgetName()
{
returnthis.name;
}
publicintgetAge()
{
returnthis.age;
}
//定义一个输出方法,可是此方法为笼统方法
publicabstractStringgetInfo();
};
我们测试一下,发现编译正常,说明笼统类可以有结构方法哈~
我们持续哈~
abstractclassPerson
{
//Person类应该有名字和年纪
privateStringname;
privateintage;
publicPerson(){}
//假如现已不是无参的,则有必要在子类中清晰调用无参结构
publicPerson(Stringname,intage)
{
this.name=name;
this.age=age;
}
publicvoidsetName(Stringname)
{
this.name=name;
}
publicvoidsetAge(intage)
{
this.age=age;
}
publicStringgetName()
{
returnthis.name;
}
publicintgetAge()
{
returnthis.age;
}
//定义一个输出方法,可是此方法为笼统方法
publicabstractStringgetInfo();
};
classStudentextendsPerson
{
publicStudent(Stringname,intage)
{
//调用Person类中有两个参数的结构方法
super(name,age);
}
//覆写父类的笼统方法
publicStringgetInfo()
{
return名字:+super.getName()+,年纪:+super.getAge();
}
};
publicclassDemo05
{
publicstaticvoidmain(Stringargs)
{
Students=newStudent(王乾,27);
System.out.println(s.getInfo());
}
}
笼统类中许可有结构方法,可是此结构方法并不是直接实例化笼统类自己的目标使的,假如在笼统类中没有清晰的无参结构方法,即:存在有参结构,则有必要在子类清晰的使用super指明要调用父类中的那个结构方法。
注意:
假如一个笼统类中没有任何一个笼统方法,则也是不能直接实例化的。
abstractclassA
{
publicvoidprint(){}
};
publicclassDemo06
{
publicstaticvoidmain(Stringargs)
{
newA();
}
};
final可以声明一个类,可是此类是绝对不能有子类的。
而笼统类又有必要被子类承继。==gt;对立的
finalabstractclassA
{
(A recap of last season's content:
1. super and this keywords
2. Method overloading and overriding
Top knowledge points this season:
1. final keywords
2. Simple understanding of general classes and interfaces (the most important part of JAVA)
final keyword
finalizer - final
1. A class marked as final cannot be inherited
finalclassA
{
};
classBextendsA
{
};
Verify it:
2. The method marked as final cannot be overridden by subclasses
finalclassA
{
publicfinalvoidprint(){}
};
classBextendsA
{
publicvoidprint(){}
};
3. The variable symbolized by final becomes a constant. If it becomes a constant, it cannot be modified later
classA
{
finalStringNAME=Hello;
publicfinalvoidprint()
{
NAME=World;
}
};
When declaring variables before, the first letter of the first word is lowercase, and the first letter of each word is uppercase. If you use final to declare constants, all words must be capitalized.
Focus: Big Picture Constants:
static: shared by all targets
final: is a constant
public: indicates that it can be seen by the outside world
publicstaticfinalStringFLAG=redking.blog.51cto;Overall constant
general class
Generic class: A class that includes a generic method is called a generic class.
General method: A method that is only declared but not implemented is called a general method.
A method without a method body (method body: "{}") is called a general method.
Except for the general method, the other definitions are the same as the general class.
General class = function of general class general method
abstractclassA
{
publicstaticfinalStringFLAG=redking.blog.51cto;
publicvoidprint()
{
//There is a method body, so it is a general method
System.out.println(HelloWorld~~~);
}
//A general method is defined here
publicabstractvoidfun();
};
publicclassDemo04
{
publicstaticvoidmain(Stringargs)
{
Aa=newA();
}
};
If you want to use a general class, you can't instantiate it directly, it is necessary for the general class to have subclasses.
The general class must be inherited. If the inherited subclass is not a general class, it must override the comprehensive general method.
abstractclassA
{
publicstaticfinalStringFLAG=redking.blog.51cto;
publicvoidprint()
{
//There is a method body, so it is a general method
System.out.println(HelloWorld~~~);
}
//A general method is defined here
publicabstractvoidfun();
};
//The subclass overrides the comprehensive general method in the general class
classBextendsA
{
publicvoidfun()
{
//super.FLAG can also be written as FLAG, because FLAG is now an overall constant ha~~~
System.out.println(FLAG= super.FLAG);
}
};
publicclassDemo04
{
publicstaticvoidmain(Stringargs)
{
Bb=newB();
b.fun();
b.print();
}
};
Verify the effect and prove that it is necessary to write the general class like this~~~
Generic class definition
General class usage rules
General Class Considerations
abstractclassPerson
{
//Person class should have name and age
privateStringname;
privateintage;
publicPerson(Stringname,intage)
{
this.name=name;
this.age=age;
}
publicvoidsetName(Stringname)
{
this.name=name;
}
publicvoidsetAge(intage)
{
this.age=age;
}
publicStringgetName()
{
returnthis.name;
}
publicintgetAge()
{
returnthis.age;
}
//Define an output method, but this method is a general method
publicabstractStringgetInfo();
};
Let's test it and find that the compilation is normal, indicating that the general class can have structural methods~
We will continue~
abstractclassPerson
{
//Person class should have name and age
privateStringname;
privateintage;
publicPerson(){}
//If it is not already parameterless, it is necessary to explicitly call the parameterless structure in the subclass
publicPerson(Stringname,intage)
{
this.name=name;
this.age=age;
}
publicvoidsetName(Stringname)
{
this.name=name;
}
publicvoidsetAge(intage)
{
this.age=age;
}
publicStringgetName()
{
returnthis.name;
}
publicintgetAge()
{
returnthis.age;
}
//Define an output method, but this method is a general method
publicabstractStringgetInfo();
};
classStudentextendsPerson
{
publicStudent(Stringname,intage)
{
//Call the structure method with two parameters in the Person class
super(name, age);
}
//Override the general method of the parent class
publicStringgetInfo()
{
return name:super.getName() , age:super.getAge();
}
};
publicclassDemo05
{
publicstaticvoidmain(Stringargs)
{
Students=newStudent(Wang Qian,27);
System.out.println(s.getInfo());
}
}
The general class is allowed to have structural methods, but this structural method is not used to directly instantiate the general class's own goal. If there is no clear no-parameter structural method in the general class, that is: there is a parameterized structure, it is necessary to The class clearly uses super to indicate which structural method in the parent class to call.
Notice:
If a general class does not have any general method, it cannot be instantiated directly.
abstractclassA
{
publicvoidprint(){}
};
publicclassDemo06
{
publicstaticvoidmain(Stringargs)
{
newA();
}
};
Final can declare a class, but this class must not have subclasses.
The general class must be inherited by subclasses. ==gt;opposite
finalabstractclassA
{)
页:
[1]