super
super和this通常是我们在继承中使用。以下对super和this进行简单初步的了解,后续会有更新。
1.super和this都是指向对象的关键字,因此不可以在static环境中使用,包括代码区块和其他。
2.super调用父类中的方法,在子类中使用
3.this是指向自己的指针。
public class UI extends JFrame{//将本类继承在JFrame父类中作为子类
public static void main(String[] args) {
new UI().UI2();
}
public void UI2(){//使用this关键字 直接作为本类的对象对本类的方法进行使用
this.setTitle("this");
this.setSize(500,500);
this.setVisible(true);
this.setDefaultCloseOperation(3);
}
public void paint(Graphics g) {
super.paint(g);//使用super关键字 调用父类中的paint方法并重写子类中的paint方法
g.drawOval(100, 100, 100, 100);
}
}
结果如图所示
我们将圆圈直接画在了窗口组件上,这是因为paint方法是jframe类的窗口绘制方法,使用super方法之后直接将父类的paint方法进行调用,并绘制了子类中重写的方法