new static 和  new self的区别

 new static    new self   都是实例化当前类, 但是new static只有代码所在的类,就是子类如果没有重写的话那么实例化的就是父类。 而new self就是严格的当前类

 

二、static::

static关键字可以实现以下功能:
1 调用类的静态方法   有后期静态绑定效果;
2 调用类的静态属性   有后期静态绑定效果;
3 调用类的非静态方法  没有后期静态绑定效果;
4 注意:不可以调用非静态属性;

static关键字可以实现以下功能:
1 调用类的静态方法   有后期静态绑定效果;
2 调用类的静态属性   有后期静态绑定效果;
3 调用类的非静态方法  没有后期静态绑定效果;
4 注意:不可以调用非静态属性;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用Java优化一下代码,将父类people类中的部分方法重写,部分成员变量隐藏。class Student{     String name;     String gender;     int age;     public Student(String name,String gender,int age){         this.name=name;   this.gender=gender;   this.age=age;     }     public String getName(){         return this.name;     }     public String getGender(){         return this.gender;     }     public int getAge(){         return this.age;     }     public void study(){         System.out.println(this.name+" is studying.");     } } class Teacher{     String name;     String gender;     int age;          public Teacher(String name,String gender,int age){         this.name=name;   this.gender=gender;   this.age=age;     }     public String getName(){         return this.name;     }     public String getGender(){         return this.gender;     }     public int getAge(){         return this.age;     }     public void teach(){         System.out.println(this.name+" is teaching.");     } } class People{     Student student;     Teacher teacher;     public People( Student student,Teacher teacher){         this.student=student;         this.teacher=teacher;     }      public Student getStudent(){         return this.student;     }     public Teacher getTeacher(){         return this.teacher;     }     public void think(){         System.out.println("I am thinking.");     } } public class MainClass{     public static void main(String args[]){         Student student=new Student("Tom","male",20);   Teacher teacher=new Teacher("Jerry","male",30);         People p=new People(student,teacher);                       System.out.println("Student:"+p.getStudent().getName()+","+p.getStudent().getGender()+","+p.getStudent().getAge());   System.out.println("Teacher:"+p.getTeacher().getName()+","+p.getTeacher().getGender()+","+p.getTeacher().getAge());   p.getStudent().study();   p.getTeacher().teach();   p.think();     } }
最新发布
05-22
以下是使用Java进行优化后的代码: class Person { private String name; private String gender; private int age; public Person(String name, String gender, int age) { this.name = name; this.gender = gender; this.age = age; } public String getName() { return this.name; } public String getGender() { return this.gender; } public int getAge() { return this.age; } public void think() { System.out.println("I am thinking."); } } class Student extends Person { public Student(String name, String gender, int age) { super(name, gender, age); } public void study() { System.out.println(this.getName() + " is studying."); } } class Teacher extends Person { public Teacher(String name, String gender, int age) { super(name, gender, age); } public void teach() { System.out.println(this.getName() + " is teaching."); } } class People { private Student student; private Teacher teacher; public People(Student student, Teacher teacher) { this.student = student; this.teacher = teacher; } public Student getStudent() { return this.student; } public Teacher getTeacher() { return this.teacher; } public void think() { System.out.println("I am thinking."); } } public class MainClass { public static void main(String[] args) { Student student = new Student("Tom", "male", 20); Teacher teacher = new Teacher("Jerry", "male", 30); People p = new People(student, teacher); System.out.println("Student: " + p.getStudent().getName() + ", " + p.getStudent().getGender() + ", " + p.getStudent().getAge()); System.out.println("Teacher: " + p.getTeacher().getName() + ", " + p.getTeacher().getGender() + ", " + p.getTeacher().getAge()); p.getStudent().study(); p.getTeacher().teach(); p.think(); } } 在这个优化后的代码中,我们将Person类中的成员变量隐藏并重写了部分方法。Student和Teacher类都继承了Person类,这样我们就可以使用继承来避免代码重复。最后,我们对People类进行了一些微小的修改,以使其与新的类结构相匹配。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值