Java学习笔记04--final关键字;抽象类;值交换;接口

===============java相关讲解=============

final关键字:

final关键字的用法

  • final关键字修饰一个基本类型的变量时,该变量不能重新赋值,第一次的值为最终的。
  • fianl关键字修饰一个引用类型变量时,该变量不能重新指向新的对象。
  • final关键字修饰一个函数的时候,该函数不能被重写。
  • final关键字修饰一个类的时候,该类不能被继承。

常量 的修饰符一般为: public static final


抽象类

抽象类的应用场景:

我们在描述一类事物的时候,发现该种事物确实存在着某种行为,但是这种行为目前是不具体的,那么我们可以抽取这种行为 的声明,但是不去实现该种行为,这时候这种行为我们称作为抽象的行为,我们就需要使用抽象类。

抽象类的好处: 强制要求子类一定要实现指定的方法。

抽象类要注意的细节:

  • 如果一个函数没有方法体,那么该函数必须要使用abstract修饰,把该函数修饰成抽象 的函数。。
  • 如果一个类出现了抽象的函数,那么该类也必须 使用abstract修饰。
  • 如果一个非抽象类继承了抽象类,那么必须要把抽象类的所有抽象方法全部实现。
  • 抽象类可以存在非抽象方法,也可以存在抽象的方法.
  • 抽象类可以不存在抽象方法的。
  • 抽象类是不能创建对象的。
    疑问:为什么抽象类不能创建对象呢?

    因为抽象类是存在抽象方法的,如果能让抽象类创建对象的话,
    那么使用抽象的对象调用抽象方法是没有任何意义的。
    
  • 抽象类是存在构造函数的,其构造函数是提供给子类创建对象的时候初始化父类的属性的。
//动物类--->抽象类
abstract class Animal{
    String name;
    String  color;
    public Animal(String  name,String color){
        this.name = name;
        this.color = color;
    }
    //非抽象的方法
    public void eat(){
        System.out.println(name+"吃粮食");
    }
    //移动...
    public abstract void run();
}

//狗 是属于动物中一种 
class Dog extends Animal{
    public Dog(String name,String color){
        super(name,color);
    }
    public void run(){
        System.out.println(name+"四条腿跑得很快...");
    }
}

//鱼 是属于动物中一种
class Fish extends Animal{
    public Fish(String name,String color){
        super(name,color);
    }
    public void run(){
        System.out.println(name+"摇摇尾巴游啊游!");
    }
}

class Demo3 {
    public static void main(String[] args) 
    {
        /*
        Dog d = new Dog("牧羊犬","棕色");
        d.run();
        //创建一个鱼对象
        Fish f  = new Fish("锦鲤","金黄色");
        f.run();
        */
        Animal a = new Animal();

    }
}

抽象类demo举例

需求: 描述一个图形、圆形、 矩形三个类。不管哪种图形都会具备计算面积与周长的行为,但是每种图形计算的方式不一致而已。

常量的命名规范:全部字母大写,单词与单词 之间 使用下划线分隔。

abstract不能与以下关键字共同修饰一个方法:

1. abstract不能与private共同修饰一个方法。
2. abstract 不能与static共同修饰一个方法。
3. abstract 不能与final共同修饰一个方法。
//abstract 抽象

//图形类
abstract class MyShape{ 
    String name;
    public MyShape(String name){
        this.name = name;
    }
    public  abstract void getArea();
    public abstract void getLength();
}

//圆形 是属于图形类的一种
class Circle extends MyShape{
    double r;
    public static final double PI = 3.14;
    public Circle(String name,double r){
        super(name);
        this.r =r;
    }
    public  void getArea(){
        System.out.println(name+"的面积是:"+PI*r*r);
    }
    public  void getLength(){
        System.out.println(name+"的周长是:"+2*PI*r);
    }
}

//矩形 属于图形中的 一种
class Rect extends MyShape{
    int width;
    int height;
    public Rect(String name,int width, int height){
        super(name);
        this.width = width;
        this.height = height;
    }
    public  void getArea(){
        System.out.println(name+"的面积是:"+width*height);
    }
    public  void getLength(){
        System.out.println(name+"的周长是:"+2*(width+height));
    }
}

class Demo4 
{
    public static void main(String[] args) 
    {
        //System.out.println("Hello World!");
        Circle c = new Circle("圆形",4.0);
        c.getArea();
        c.getLength();

        //矩形
        Rect r = new Rect("矩形",3,4);
        r.getArea();
        r.getLength();

    }
}

抽象类详解博客


值交换的几种方式


接口

接口:拓展功能的。 usb接口.。。

接口的定义格式:

interface 接口名{

}

接口要注意的事项 :

1. 接口是一个特殊的类。
2. 接口的成员变量默认的修饰符为: public static final 。
   那么也就是说接口中的成员变量都是常量。
3. 接口中 的方法都是抽象的方法,默认的修饰符为: public abstract。
4. 接口不能创建对象。
5. 接口是没有构造方法的。
6. 接口是给类去实现使用的,非抽象类实现一个接口的时候,
   必须要把接口中所有方法全部实现。

实现接口的格式:

class  类名 implements 接口名{

}

interface A{
    //成员变量
    public static final int i=10;
    //成员函数
    public void print();
}
class Demo7 implements A{ // Demo7就实现了A接口
    public static void main(String[] args) 
    {
        Demo7 d = new Demo7();
        d.print();
    }
    //实现接口中的方法
    public void print(){
        System.out.println("这个是接口中的print方法...");
    }
}

接口的作用:

1. 程序的解耦。  (低耦合)
2. 定义约束规范。
3. 拓展功能。

代码举例:

//普通的铅笔类
class Pencil{
    String name;
    public Pencil(String name){
        this.name = name;
    }
    public void write(){
        System.out.println(name+"沙沙的写...");
    }
}

//橡皮接口
interface Eraser{
    public void remove();
}

//带橡皮的铅笔
class PencilWithEraser extends Pencil implements Eraser {
    public PencilWithEraser(String name){
        super(name);
    }
    public void remove(){
        System.out.println("涂改,涂改....");
    }
}

class Demo8 
{
    public static void main(String[] args) 
    {
        PencilWithEraser p = new PencilWithEraser("2B铅笔");
        p.write();
        p.remove();

    }
}

类与接口之间关系: 实现关系。

类与接口要注意的事项:

1. 非抽象类实现一个接口时,必须要把接口中所有方法全部实现。
2. 抽象类实现一个接口时,可以实现也可以不实现接口中的 方法。
3. 一个类可以实现多个接口 。

疑问: java为什么不支持多继承,而支持了多实现呢?

        class A{

            public void print(){
                System.out.println("AAAAAA");
            }
        }

        class B{

            public void print(){
                System.out.println("BBBBBB");
            }
        }


        class C extends A ,B{

        }

        new C().print();

接口与接口之间关系: 继承关系。

接口与接口之间要注意事项:

1. 一个接口是可以继承多个接口的。
interface A{
    public void print();
}
interface C{
    public void getArea();
}
interface B extends A,C{ // B接口继承A接口
    public void test();
}

class Demo10 implements B{
    public static void main(String[] args) 
    {
        Demo10 d = new Demo10();
        d.print();
    }
    public void getArea(){}
    public void test(){}
    public void print(){
        System.out.println("这个是A接口的print方法...");
    }
}

猜你喜欢

转载自blog.csdn.net/liyunxiangrxm/article/details/81005282