自定义类要导包 不然会报错
package com.xx.study; import java.util.HashMap; public class Person { public static void main(String[] args) { HashMap map =new HashMap(); } }当属性重名是,super调用的是父类的,this先找子类的 Person.java
package com.xx.study; public class Person { String name; int age; int id=1001;//身份证号 public Person() {} public Person(String name,int age) { this.name=name; this.age=age; } public void eat() { System.out.println("吃饭"); } public void sleep(int time) { System.out.println("睡觉"+time+"小时"); } }Student.java
package com.xx.study; public class Student extends Person { String major; //和父类都有id,内存中两个id,属性和方法不一样,方法会覆盖,属性不会 int id=1002;//学号 public Student() { } public Student(String major) { this.major = major; } public void study() { System.out.println("学习java"); } // 对父类的eat方法重写 public void eat() { System.out.println("吃减脂餐"); } public void show() { //System.out.println(name+"年龄是"+age); //可以用this调用也可以用super, System.out.println(this.name+"年龄是"+super.age); System.out.println(id); System.out.println(this.id);//调用的是子类的id System.out.println(super.id);//调用的是父类的id } }SuperTest.java
package com.xx.study; public class SuperTest { public static void main(String[] args) { Student s=new Student(); s.show(); } }4.4 在构造器的首行,没有显示的声明“this(形参列表)”或super(形参列表)”,则默认调用的是父类中空参的构造器,默认有一个super()
Person.java
package com.xx.study; public class Person { String name; int age; int id=1001;//身份证号 public Person() { System.out.println("测试在子类构造器中不声明this,或super,默认调用super()"); } public Person(String name,int age) { this.name=name; this.age=age; } public void eat() { System.out.println("吃饭"); } public void sleep(int time) { System.out.println("睡觉"+time+"小时"); } }Student.java
package com.xx.study; public class Student extends Person { String major; //和父类都有id,内存中两个id,属性和方法不一样,方法会覆盖,属性不会 int id=1002;//学号 public Student() { } public Student(String major) { //super();默认有 this.major = major; } public Student(String name,int age,String major) { //this.name=name; //this.age=age; super(name,age);//调用父类的构造器 ctrl+鼠标左键 this.major=major; } public void study() { System.out.println("学习java"); } // 对父类的eat方法重写 public void eat() { System.out.println("吃减脂餐"); } public void show() { //System.out.println(name+"年龄是"+age); //可以用this调用也可以用super, System.out.println(this.name+"年龄是"+super.age); System.out.println(id); System.out.println(this.id);//调用的是子类的id System.out.println(super.id);//调用的是父类的id this.eat(); super.eat(); } }SuperTest.java
package com.xx.study; public class SuperTest { public static void main(String[] args) { Student s=new Student(); s.show(); Student s1=new Student("Jack马",55,"阿里"); s1.show(); System.out.println("********************"); Student s2=new Student(); s2.show(); } }原因在于其生命周期:先是类的加载,静态结构也跟着加载到内存当中,创建对象后对象出生,非静态结构加载,对象消亡,非静态结构就不能调用了,最后类消亡,此时静态结构也消亡 结论:静态结构跟着类加载消亡,非静态结构是跟着对象加载消亡,因此早出生的不能调晚出生的,晚出生的可以调早出生的
package com.xx.study1030; public class StaticTest { public static void main(String[] args) { Chinese c1 = new Chinese(); c1.name = "马化腾"; c1.age = 20; Chinese c2 = new Chinese(); c2.name = "马云"; c2.age = 50; c1.nation = "中国"; System.out.println(c2.nation); c1.eat(); //报错 Chinese.eat(); //静态方法可以用类调用 Chinese.show(); } } class Chinese { String name; int age; static String nation; public void eat() { System.out.println("吃米饭"); //调用非静态结构 //谁调用eat谁就是this,就输出谁的name,这个谁指的对象 this.info(); //调用静态结构 walk(); //nation前面省略的是类Chinese Chinese.nation System.out.println("nation:"+nation); } //随着类的加载而加载 public static void show() { //只能调用静态方法或属性 eat(); System.out.println("我是静态方法"); //类调用,不是this调用 System.out.println(Chinese.nation); //省略 System.out.println(nation); walk(); } public static void walk() { System.out.println("走路"); } public void info() { System.out.println(name); } }属性是可以被多个对象所共享的,不会随着对象的不同而不同的 类中的常量也常常声明为static
操作静态属性的方法,通常设置为static 工具类中的方法,习惯上声明为static,这样就不用了new对象了,直接用类调用 例如:Math,Arrays,Collection
return ++x是错误的,这个是先++,再赋值,return x+1是可以的。
属性是可以变的,对象不能变,所以o=new Other()错了,o.i++是对的
