1.抽象类
概念:当一个类中没有足够的信息去描述一个具体事物时,那么这个类就是抽象类。
注意:抽象类一般都是对概念类别的描述,不过这个概念下存在着N多具体的事物。
语法:
[访问修饰符] abstract class 类名{
}
<strong><span style="font-family:SimHei;">/**
抽象类 应用示例
*/
//定义一个图形类
abstract class Shape{
int width;
int height;
public Shape(int width,int height){
this.width=width;
this.height=height;
}
//定义抽象方法
public abstract void area();
public abstract void zhouChang();
public void display(){
System.out.println("抽象类中的普通方法");
}
}
class Chang extends Shape{
public Chang(int width,int height){
super(width,height);
}
public void area(){
System.out.println("长方形的面积:"+super.width*super.height);
}
public void zhouChang(){
System.out.println("长方形的周长:"+(super.width+super.height)*2);
}
}
public class Test1{
public static void main(String[]args){
Chang c=new Chang(10,20);
c.area();
c.zhouChang();
}
}</span></strong>
特点:
在抽象类中可以存在构造方法,但是抽象类不能直接创建对象。
抽象类中可以存在普通属性和普通方法或者静态方法。
抽象类中还可以存在抽象方法,抽象方法只有方法的声明,没有方法体,而且抽象方法不能是静态的。
抽象方法的定义语法:
[访问修饰符] abstract 返回值类型 方法名称(参数列表);
抽象类中的抽象方法,需要有子类来实现;如果子类没有实现父类中的抽象方法,则有子类的子类继续实现。
如果一个类中存在一个抽象方法,那么这个类一定是抽象类;但是抽象类中不一定有抽象方法。
抽象类中的构造方法是用来子类创建对象时,由JVM来调用创建父类的对象。
2.接口
接口就是对外提供了一系列功能的声明,也就是说在接口中只有方法的声明,没有方法体。
特点:
接口可以实现多继承。
用interface关键字来声明接口。
接口中不可以有构造方法。
接口中定义的变量都是常量,因为默认情况下接口中所有的变量都默认是public static final来修饰的
接口中所有的方法只能用public来修饰。
接口中定义的方法需要有类来实现。
语法:
[访问修饰符] interface 接口名{
}
<strong><span style="font-family:SimHei;">/**
接口 应用示例
*/
interface inter01{
int num=100;
public void display();
}
interface inter02{
public void test();
}
interface inter03 extends inter01,inter02{
public void test2();
}
class Impl implements inter03{
public void display(){
System.out.println("ok1");
}
public void test(){
System.out.println("ok2");
}
public void test2(){
System.out.println("ok3");
}
public void t(){
System.out.println(num);
System.out.println(inter01.num);
}
}
public class Test2{
public static void main(String[]args){
Impl impl=new Impl();
impl.display();
impl.test();
impl.test2();
impl.t();
}
}
</span></strong>
用关键字 implements 实现接口。
如:
class Car implements Runner 每个类只能有一个超类,但可以实现多个接口。
如果实现多个接口,则用逗号隔开接口名称
如下所示:
class Car implements Runner, Constants一个类实现了一个接口,它必须实现接口中定义的所有方法,否则该类必须声明为抽象类。接口可以继承自其它的接口,并添加新的常量和方法。
接口支持多重继承。
<strong><span style="font-family:SimHei;">/**
*/
interface MobilePhone{
public void call();
public void receive();
public void sendMsg();
public void receiveMsg();
}
interface Camera{
public void takePhoto();
}
interface CameraPhone extends MobilePhone,Camera{
}
/**
类实现接口,这里一定要注意,该类实现了哪个接口,就必须将这个接口中的所有方法具体化。
*/
class NokiaPhone implements CameraPhone{
public void call(){
System.out.println("使用Nokia打电话");
}
public void receive(){
System.out.println("使用Nokia接电话");
}
public void sendMsg(){
System.out.println("使用Nokia发送短信");
}
public void receiveMsg(){
System.out.println("使用Nokia接收短信");
}
public void takePhoto(){
System.out.println("使用Nokia拍照");
}
}
class MotoPhone implements CameraPhone{
public void call(){
System.out.println("使用Moto打电话");
}
public void receive(){
System.out.println("使用Moto接电话");
}
public void sendMsg(){
System.out.println("使用Moto发送短信");
}
public void receiveMsg(){
System.out.println("使用Moto接收短信");
}
public void takePhoto(){
System.out.println("使用Moto拍照");
}
}
class Student{
String name;
CameraPhone myPhone;
public Student(String name,CameraPhone myPhone){
this.name=name;
this.myPhone=myPhone;
}
public void myCall(){
myPhone.call();
}
}
public class TestInterface{
public static void main(String[]args){
Student stu1=new Student("zhangsan",new NokiaPhone());
stu1.myCall();
Student stu2=new Student("李四",new MotoPhone());
stu2.myCall();
}
}
</span></strong>
3.static关键字
实例变量:只有创建了类的对象,才会为变量分配内存。
实例变量属于对象,所以需要通过对象访问。
static可以修饰变量,方法,静态代码块。
<strong><span style="font-family:SimHei;">/**
static 关键字
*/
public class Test3{
static int num;
static{
num=100;
}
public static void main(String[]args){
System.out.println(Test3.num);
}
public static void display(){
}
public void t(){
display();
}
}</span></strong>
①.static修饰的变量称为静态变量(类变量)。
静态变量不属于某个对象,而是属于类,因为JVM在加载类时,就会为类中所有静态的变量或者方法进行开辟相应的内存空间,从某种意义上来说类中被static修饰的代码,只加载一次。
静态变量一般用类名.变量名的方式进行访问。
②.static修饰的方法称为静态方法(类方法).
静态方法只能访问静态属性或者静态方法。
在非静态方法中,可以访问静态方法也可以直接调用普通方法。
在静态方法中,不能使用this和super关键字。
③.static修饰代码块称为静态代码块,语法:
static{
}
static静态代码块位于类中,任何方法的外面。
作用:就是用来初始化成员静态变量的或者需要在类加载时进行初始化的内容。