this super

来源:互联网 发布:ubuntu怎么上传lrzsz包 编辑:程序博客网 时间:2024/05/01 00:45

-----------------------------------------------------

this 是一个在heap里面的 指向自己的变量

指向对象自身的引用,类型 private 因为只能在自身里面访问,类似成员变量

------------------------------------------------

非静态不能访问静态 

this能访问静态 因为this是指向自身的一个引用 通过对象访问静态成员

-----------------------------------------------

静态方法只能访问静态成员 不能访问成员变量

class Benz

{

  int tires; 

  static String BRAND ="BENZ"

  public static void outBrand(){

  print(tires); // 编译错误 无法从静态上下文引用静态变量 静态方法只能访问静态成员

  System.out.printlin(this.tires); //静态方法都不一定有对象 编译错误 this可以理解成员变量

}

}

静态方法不能写this super

--------------------------------------------------------------

public Benz(){

  print();

}


public Benz(){

  this(); //调用构造函数 不带参数

  this.color = color;

}

public Benz(){

  this(color); //调用必须放在第一句 上下不能对调

  this.thires=tires;  

}

----------------------------------

this() 访问当前类的构造函数 //this() 要放在构造函数第一句  this() 这里是构造函数 为了构造函数的重用

this.xxx 成员变量 this这里是指针 引用

---------------------------------------

java不支持多重继承 c++支持多重继承

--------------------------------------

子类可以访问父类的非private 属性 跟 行为

private 不让被看见使用 不被继承

-------------------------------------------

class ClassDemo5{

  public static void main(String[] args ){

  Dog d = new Dog();

  d.color = "yellow";

  Jing8 dd= new Jing8();

  dd.bar(); 

  }

}


class Animal{

  String color;

  int height;

  public Animal(String color){

    this.color=color; 

  }

  public void bar(){

  print("wwww");

  }

}


//class Cat extends Animal{

//  String name;

//}

//extends 继承 

class Dog extends Animal{

  public Dog(){

    super("white");

  }

  public void bar(){

   super.bar(); //可以调换位置 必须要写第一行 是super() 构造函数 而不是super 

   print("wow");

  }

}

//多重继承

class Jing8 extends Dog{

  public Jing8{

    super();

  }  //省略 调用 dog的 构造函数 dog 用animal的调用函数 调用animal空函数 但是 构造函数空函数已经没了,所以会报错

  String owner;

}

------------------------------------

throws 内声明时候关键字  throw是动词跑出异常

-------------------------------------

继承

1.extends

2.单继承

3.多层继承

----------------

super() 访问超类构造函数,必须是构造函数第一行,构造函数第一行要么是this(),要么是super(),默认是super(

---------------------

方法重载 method overload 在一个类中 方法名相同 参数类或者个数不同

方法重写 覆盖 overwrite 把父类行为覆盖

-------------------

super 跟 this 都可以理解成类内部的成员变量 指向不同的类型

-----------------------------------------------

super.xx 访问超类的成员

当子父类出现同名成员变量,可以用super区分

-----------------------------

成员函数 次重要

成员变量重要

1. 成员变量就是数据 最重要 所以 成员变量不能覆盖 成员函数可以覆盖

--------------------------------------

函数覆盖 重写 复写

1.overwrite

2.只有在继承时候才有重写

3.父类的私有方法不可以被覆盖,因为都不能被继承

4.

--------------------------------------------------

原创粉丝点击