• Java笔记


    1.Extends 关键字: 

    /*The Extends Keyword
     * when a class is a hyponym of an interface, we used implements.
     * 1.Example: SLList<Blorp> implements List61B<Blorp>
     *
     * If you want one class to be a hyponym of another class,
     * you use extends.
     *
     * We'd like to build RotatingSLList that perform any SLList 
     * operation as well as:
     *
     */
    /*SLList, but with additional rotateRight operation.*/
    public class RotatingSLList<Item> extends SLList<Item> {
    
        /*To do: Implement RotatingSLList such that 
         * code compiles and outputs correct result.*/
        
        public void rotateRight(){        
            Item x = removeLast();
            addFirst(x);
        }
    
    /*Because of extends, RotatingSLList inherits all members 
     * of SLList: 
     * (1) All instance and static variables;
     * (2) All methods;
     * (3) All nested classes.  
     * Contructors are not inherited.
     * But members may be private and thus inaccessible!  
     *
     * */
    
        public static void main(String[] args) {
            RotatingSLList<Integer>    rsl = new RotatingSLList<>();    
            /*Creates SList:[10, 11, 12, 13]*/
            rsl.addLast(10);
            rsl.addLast(11);
            rsl.addLast(12);
            rsl.addLast(13);
            
            /*Should be: [13, 10, 11, 12]*/
            rsl.rotateRight();
            rsl.print();
    
        }
    }

    2. 

    /*
     *SList with additional operation printLastItems() 
     * which prints all items;
     */
    
    public class VengefulSLList<Item> extends SLList<Item> {
        SLList<Item> deletedItems;
        /*Initialize
         *SLList<Item> deletedItems = new SLList<Item>();
         */
    
        public VengefulSLList() {
            deletedItems = new SLList<Item>();
        }
    
    
        @Override
        public Item removeLast(){
            /*Can access private member
             * calls Superclass's version of removeLast()
             * */
            Item x = super.removeLast();
            deletedItems.addLast(x);
            return x;
        }
    
        /*Prints deleted items*/
        public void printLostItems(){
            deletedItems.print();
        }
    
    }

    3. Constructor 的继承。

    /*Constructors are not inheried. However, the rules 
     * of Java say that all constructors must start with a call 
     * to one of the super class's constructors.
     *Idea: If every VengefulSLList is an SLList, 
     every VengefulSLList must be set up like a SLList.
     *You can explictly call the constructor with the keyword super.
     **if you don't explictly call the constructor, 
     Java will automatically do it it for you. 
     * */
    
    public VengefulSLList() {
        deletedItems = new SLList<Item>();
    }
    public VengefulSLList() {
        super();
        deletedItems = new SLList<Item>();
    }
    
    
    /*Calling other Constructors*/
    /*If you want to use a super constructor other than 
     *the no-argument constructor, can give parameters to super.
     * */
    
    public VengefullSLList(Item x) {
        super(x); //calls SLList(Item x)
        deletedItems = new SLList<Item>();
    }
    
    /*Here makes implict call to super(), not super(x)*/
    public VengefulSLLis(Item x) {
        deletItems = new SLList<Item>();
    }

    4. Compile-time(static type) : 

    /*Compile-Time Type Checking
     * Compiler allows methods calls based on compile-time type
     * also called static type.
     * of variable.
     * 1):sl's runtime type: VengefulSLList.
     * 2)But cannot call printLostItems. 
     */
    
    public static void main(String[] args) {
        VengefulSLList<Integer> vsl = 
            new VengefulSLList<Integer>(9);
        SLList<Integer> Sl = vsl;
    }
    
    /*Compile-Time Types and Expressions*/
    /*Expressions have compile-time types:
     * An expression using the new keyword has the 
     * specified compile-time type. 
     */
    SLList<Integer> sl = new VengefulSLList<Integer>();
    
    /*A: Compile-time type of RHS expression is VengefulSLList. 
     *B: A VengefulSLList is-an SLList, so assignment is allowed.
     */
    
    VengefulSLList<Integer> vsl = new SLList<Integer>();
    /*A. Compile-time type of RHS expression is SLList. 
     *B. An SLList is not necessarily a VengefulSLList, 
     so compilation error results.
     */
    
    /*Casting 
     *Java has a special syntax for forcing the compile-time
     *type of any expression.
     *Put  desired type in parenthesis before the expression. 
     *Example: 
    
     Compile-time type Dog: maxDog(frank, frankJr);
     Compile-time type Poodle: (Poodle) maxDog(frank, frankJr);
     * */ 

    5. Object class: 

    /*The object Class*/
    /*As it happens, every type in Java is 
     * a descendant of the Object class.
     *1) VengefulSLList extends SLList. 
     *2) SLList extends Object(implictly).
     */
    The Safest Way to Get what you Want is to Try and Deserve What you Want.
  • 相关阅读:
    Python代写利用LSTM模型进行时间序列预测分析
    R语言代写使用K-Means聚类可视化纽约市WiFi访问
    BZOJ 4448: [Scoi2015]情报传递 DFS序+主席树
    BZOJ 2213: [Poi2011]Difference 细节题
    CF1268B Domino for Young 黑白染色
    BZOJ 3727: PA2014 Final Zadanie 树形DP
    BZOJ 4726: [POI2017]Sabota? 树形dp
    CF1137F Matches Are Not a Child's Play LCT+树状数组
    BZOJ 2238: Mst DFS序+KDtree
    CF1111C Creative Snap 线段树
  • 原文地址:https://www.cnblogs.com/Shinered/p/10447707.html
Copyright © 2020-2023  润新知