• 函数构造,修饰符


    构造函数:

    • 构造函数是类中的一种特殊函数,构造函数的名字与类的名字相同,而且没有类型。
    • 构造函数负责对类对象的特定属性初始化。【比如每一只狗对象都有不同的名字,而狗类是一个模板,是没有名字的,而构造函数可以做到给狗定义名字。】
    • 类中可以有多个构造函数,它们以函数重载的方式存在。
    • 构造函数是没有类型的,是直接以“ 类名(){}” 作为函数。
    • 类中会默认有一个无参数的构造函数,如果定义了其他构造函数,那么这个默认的无参构造函数就会不默认存在了。
    复制代码
    class Dog{
        String name;
        Dog(){
            this.name="旺财";
        }
        Dog(String name){
            this.name=name;
        }
    }
    public class Init_usage {
    
        public static void main(String args[]) {
            
            Dog d3=new Dog();
            Dog d4=new Dog("小菜");
            System.out.println(d3.name);
            System.out.println(d4.name);
        }
    }
    复制代码

    补充:

    • 构造函数之间可以相互调用,但要防止递归调用。
      • image
      • image
      • 调用其他构造函数来实现初始化在有多个变量需要初始化的时候有明显的代码节省。

      封装与私有:

    • 类的封装的一个体现是变量和函数的私有化
    • 封装的准则:
      • 将不需要对外提供的内容都隐藏起来。
      • 将属性都隐藏,提供公共方法对其访问。
    • 私有化使类的非公共属性隐藏了起来,比如一个“人类”对象有自己的资产,一般只有自己才能知道自己有多少资产,并且不想直接给别人看到,但如果别人问题,还是会告诉别人的(隐藏了自己的资产情况,但是自己能够使用方法获得结果,这个方法是开放的,调用就是相当于别人问)。
    • 可以使用private来使变量和函数私有化,这样之后不能直接使用 对象.变量 或 对象.函数 来调用,只有对象内部的方法才能调用
    • 将变量或方法私有化之后,一般对外提供getXXX,setXXX方法进行访问,提高数据访问的安全性。
    复制代码
    class Man{
        private int money;
        String name;
        Man(String name,int money){
            this.name=name;
            this.money=money;
        }
        int getmoney(){
            return money;
        }
        void setMoney(int money){
            this.money=money;
        }
    
        
        
    }
    
    public class Private_usage {
    
        public static void main(String[] args) {
            Man m=new Man("lilei",2000);
            System.out.println(m.name);//lilei
    //        System.out.println(m.money);//报错的,因为私有了,不能访问
    //        System.out.println(m.wife);//报错的,因为私有了,不能访问
            System.out.println(m.getmoney()); //2000
            m.setMoney(6000);
            System.out.println(m.getmoney());//6000
    
        }
    
    }
    复制代码

    this关键字:

    • this代表当前对象(调用函数时代表当前调用该函数的对象),比如在类中可以使用this.XXX来调用对象自己的变量或方法。
    • 当局部变量和成员变量同名时,可以用this关键字区分,this.XXX代表使用对象自身的变量
    • 类中的成员变量默认是带有this前缀的,但遇到同名时必须加以区分。

    • this加上参数列表(this(参数))的方式就是访问本类中符合该参数的构造函数

    • 用于调用构造函数的this语句必须放在第一行,因为初始化动作要优先执行

    复制代码
    class Person{
        String name;
        int age;
        Person(String name,int age){
            this.name=name;
            this.age=age;
        }
        void hello() {
            this.sysprint();
    //        sysprint();
        }
        void sysprint() {
            System.out.println("hello world!");
        }
    }
    
    public class This_usage {
        public static void main(String args[]) {
            Person p1=new Person("lilei",18);
            p1.hello();//hello world!
            
        }
    
    }
  • 相关阅读:
    网络结构解读之inception系列五:Inception V4
    网络结构解读之inception系列四:Inception V3
    网络结构解读之inception系列三:BN-Inception(Inception V2)
    网络结构解读之inception系列二:GoogLeNet(Inception V1)
    网络结构解读之inception系列一:Network in Network
    深度学习相关转载收集
    Person Re-identification 系列论文笔记(八):SPReID
    Person Re-identification 系列论文笔记(七):PCB+RPP
    Person Re-identification 系列论文笔记(六):AlignedReID
    Spring@Autowired注解与自动装配
  • 原文地址:https://www.cnblogs.com/mms912/p/8734936.html
Copyright © 2020-2023  润新知