• 30天代码day4 Class vs. Instance


    Class

    A blueprint defining the charactaristics and behaviors of an object of that class type. Class names should be written in CamelCase, starting with a capital letter.

    class MyClass{
        ...
    }
    

    Each class has two types of variables: class variables and instance variables; class variables point to the same (static) variable across all instances of a class, and instance variables have distinct values that vary from instance to instance.

    Class Constructor

    Creates an instance of a class (i.e.: calling the Dog constructor creates an instance of Dog). A class can have one or more constructors that build different versions of the same type of object. A constructor with no parameters is called a default constructor; it creates an object with default initial values specified by the programmer. A constructor that takes one or more parameters (i.e.: values in parentheses) is called a parameterized constructor. Many languages allow you to have multiple constructors, provided that each constructor takes different types of parameters; these are called overloaded constructors.

    class Dog{ // class name
        static String unnamed = "I need a name!"; // class variable
        int weight; // instance variable
        String name; // instance variable
        String coatColor; // instance variable
        
        Dog(){ // default constructor
            this.weight = 0;
            this.name = unnamed;
            this.coatColor = "none";
        }
        Dog(int weight, String color){ // parameterized constructor
            // initialize instance variables
            this.weight = weight; // assign parameter's value to instance variable
            this.name = unnamed; 
            this.coatColor = color; 
        }
        Dog(String dogName, String color){ // overloaded parameterized constructor
            // initialize instance variables
            this.weight = 0;
            this.name = dogName; 
            this.coatColor = color; 
        }
    }
    

    Method

    A sort of named procedure associated with a class that performs a predefined action. In the sample code below, returnType will either be a data type or if no value need be returned. Like a constructor, a method can have or more parameters.

    returnType methodName(parameterOne, ..., parameterN){
        ...
        return variableOfReturnType; // no return statement if void
    }
    

    Most classes will have methods called getters and setters that get (return) or set the values of its instance variables. Standard getter/setter syntax:

    class MyClass{
        dataType instanceVariable;
        ...
        void setInstanceVariable(int value){
            this.instanceVariable = value;
        }
        dataType getInstanceVariable(){
            return instanceVariable;
        }
    }
    

    Structuring code this way is a means of managing how the instance variable is accessed and/or modified.

    Parameter

    A parenthetical variable in a function or constructor declaration (e.g.: in int methodOne(int x), the parameter is int x).

    Argument

    The actual value of a parameter (e.g.: in methodOne(5), the argument passed as variable x is 5).


    Additional Language Resources

    Python Class and Instance Variables

  • 相关阅读:
    LINUX怎么远程连接ORACLE数据库
    Debian 6配置GNOME桌面环境
    简简单单安装debian桌面工作环境
    NeHe OpenGL教程 第四课:旋转
    NeHe OpenGL教程 第三课:颜色渲染
    NeHe OpenGL教程 第二课:多边形
    NeHe OpenGL教程 第一课:OpenGL窗口
    Oracle数据库 External component has thrown an exception
    9. Ext基础1 -- Ext中 getDom、get、getCmp的区别
    8. Ext文本输入框:Ext.form.TextField属性汇总
  • 原文地址:https://www.cnblogs.com/helloworld7/p/10474432.html
Copyright © 2020-2023  润新知