<JAVA - 面向对象(三)>

01、单例模式:

package danli;
/**
 *     三星Note7:
 *         手机的型号:        note7
 * @author 韦桂金
 *    
 *    懒汉式:
 *        特点: 节省空间
 *
 *    饿汉式:
 *        特点: 第一刻就把对象创建出来,等待被使用
 */
public class Note7 {
    
    static Note7 note;    // 在内存中开辟一个空间,存放Note自定义数据类型的变量note
    
    // 无参构造
    public Note7() {
        
    }
    // 创建方法
    public static Note7 st() {
        if(note == null) {
            note = new Note7();
            //Note7 = new Note7();
        }
        return note;
    }
}
/**
 *   饿汉式:
 * @author 韦桂金
 *
 */
    class Note2 {   // 写一个类
        static Note2 note2 = new Note2();  // static在类加载的时候加载
        
        public static Note2 getNote2() {
            return note2;
        }
    }
package danli;
/**
 *     note7:
 *         note这个手机只能创建一个
 * @author 韦桂金
 *
 */
public class Note7Test {
    
    public static void main(String[] arge) {
        // 方法的获取方法    对象名.方法名
        Note7 note1 = Note7.st();
        Note7 note2 = Note7.st();
        Note7 note3 = Note7.st();
        Note7 note4 = Note7.st();
        
        System.out.println(note1);
        System.out.println(note2);
        System.out.println(note3);
        System.out.println(note4);
        
        
        System.out.println("=========分隔符==================");
        Note2 st = Note2.getNote2();
        Note2 nt = Note2.getNote2();
        Note2 so = Note2.getNote2();
        Note2 fe = Note2.getNote2();
        
        System.out.println(st);
        System.out.println(nt);
        System.out.println(so);
        System.out.println(fe);
    }
}

 

02、继承:

package extend;
/**
 *  爷爷的类
 * @author 韦桂金
 *
 */
public class GrandFather {
    int money = 1200;
}

 

package extend;
/**
 *     父类:
 * @author 韦桂金
 *
 */
public class father extends GrandFather{
    int age = 42;
    String name = "镇魔"; 
    char gender;
    public father(int age) {
        this.age = age;
    }
    public father() {
        
    }
    //static String name = "针对";
     public void eat() {
         System.out.println("我爱吃芹菜.....");
     }
}
package extend;
/**
 *  子类
 * @author 韦桂金
 *
 */
public class Son extends father {
    String name;
    int age = 12;
    /*public Son(int age,char gender,String name){ // 构造函数
        
    }*/
    @Override
    public void eat() {
        super.eat();
    }
    
    public Son(){
        super(30);    // 调用父类的无参构造
    }
    
    
    public void show() {
        System.out.println(this.age);
        System.out.println(super.age);
        System.out.println(this.name);
        System.out.println(super.name);
    }
    
    
}
package extend;
/**
 *     继承:
 *         当我们想要一个模板,实现多种对象,把代码取出来放到一个单独的类中  -->这个存放的类叫作父类
 *                  父类(超类,基类):    被继承者
 *                 子类(派生类):            继承者
 *         语法:
 *             public class 子类名 extends 父类名{
 *                         
 *             }
 *     注意事项:
 *         1.一个只能直接继承一个类,但是可以间接继承其他类
 *         2.一个类如果没有明确extends另一个类,那么默认继承Object类:一个类,是所有类的超类
 * 
 * 范围:
 *         子类可以继承父类的所有数据,包括私有数据,但是要间接访问,构造函数不能继承
 * 
 * 好处:
 *         省代码
 * 
 * 方法重写:
 *         如果子类和父类方法名完全相同,子类的这个方法就叫做方法的重写
 * 
 * 重写需要注意的地方:
 *             1.构成重写的条件:方法的返回值,方法名,参数列表要完全相同,修饰符的范围不能小于父类
 *             2.私有的方法和构造函数不能被重写
 *             3.静态方法不存在重写的概念,但是可以
 *     
 * @author 韦桂金
 *
 */
public class ExtendTest {
    public static void main(String[] args) {
        // 创建对象
        Son son = new Son();
        son.eat();
        
        System.out.println(son.money);
        System.out.println(son.name);
    }
}

 

03、super:

package extend;
/**
 *     java中关键字super()::
 *         thsi():       指的是当前对象的引用
 *         super(): 指的是当前对象的父类的引用
 * 
 *     两种用途:
 *         1.调用父类中的普通方法(方法和属性),如果父类和子类有相同的同名成员时,super
 *         2.在子类的构造方法中,super可以显示的调用父类的构造,用于传参
 * 
 *         在子类的构造方法中必须含有父类的构造方法(没有的话久系统默认)
 * super 必须在构造函数的第一句
 * 
 *     
 * @author 韦桂金
 *
 */
public class SuperTest {
    
    public static void main(String[] args) {
        // 创建对象
        Son son = new Son();
        son.show();
    }
}

 

04、final:

package extend;
/**
 *         java中关键字final:
 *             最终的,是一个修饰符
 * 
 *         作用:
 *             1.final 修饰类        (不能被继承)
 *             2.final 修饰方法        (不能被重写)
 *             3.final 修饰变量        (值不能修改  --常量)
 * @author 韦桂金
 *
 */
public class FinalDemo {
    
    static final int a = 8;
    
    public static void main(String[] args) {
        final int a = 5;
        
        System.out.println(a);
        System.out.println(FinalDemo.a);   // 类名.常量名(一般有static,就可以用)
    }
}
posted @ 2018-01-25 16:06  让优秀成为一种习惯  阅读(154)  评论(0编辑  收藏  举报