import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DrawCircle extends JFrame {
private final int OVAL_WIDTH = 80;// 圆形的宽
private final int OVAL_HEIGHT = 80;// 圆形的高
public DrawCircle() {
super();
initialize();// 调用初始化方法
}
// 初始化方法
private void initialize() {
this.setSize(300, 200);// 设置窗体大小
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置窗体关闭模式
setContentPane(new DrawPanel());// 设置窗体面板为绘图面板对象
this.setTitle("绘图实例2");// 设置窗体标题
}
public static void main(String[] args) {
new DrawCircle().setVisible(true);
}
// 创建绘图面板
class DrawPanel extends JPanel {
public void paint(Graphics g) {
super.paint(g);
g.drawOval(10, 10, OVAL_WIDTH, OVAL_HEIGHT); // 绘制1个圆形
}
}
}
I just want to ask , this code inside the two super statement What is the role ? After deletion , the results seemingly nothing changes ! What used to thinking of it ?
------ Solution ---------------------------------------- ----
first super constructor in DrawCircle years, so the role is to call the parent class constructor without parameters , the second super class in DrawPanel paint method , and is super.paint (g ) is to call the parent class's paint method , you say nothing changes after removal of the results that may DrawCircle is the parent class constructor and DrawPanel paint method for your results may just not work, such as the first one is to call the parent class constructor super function , which is an empty method , this time just as you do not affect the results , the use of super is hoped that the implementation of the parent class method
------ Solution ------------- -------------------------------
occurs when the class inheritance relationship,
subclass new object, we must first look at what the parent class data to be initialized because the subclass to use parent class's data, so the first line to the constructor . We all know that the system will default to provide a default no- argument constructor , when the parent class has no constructor with parameters , sub-class new object, you do not write with write super (), it does not matter , because the system provides . But when a parent class constructor with arguments , which is a subclass of new objects , according to your needs to specify the parent class constructor which has parameters , colleagues parent class constructor without parameters to display write out , or the compiler will complain .
------ Solution ---------------------------------------- ----
http://blog.sina.com.cn/s/blog_6d315ffd01014c0s.html
proposed landlord facie this
------ For reference only ----------------------------- ----------
plus super is to call the parent class's paint (Graphics g) method , without words is called a subclass .
here if not super , then I do not believe your program will not complain . Apparent infinite loop
public void paint (Graphics g) {
super.paint (g);
g.drawOval (10, 10, OVAL_WIDTH, OVAL_HEIGHT); ; / / Draw a circle
}
------ For reference only ----------------- ----------------------
constructor is only the first line of super, or this, you do not add the words , the compiler automatically add super ( );
------ For reference only ------------------------------------ ---
try it yourself to know ~
------ For reference only --------------------- ------------------
try it yourself know ~
------ For reference only ----- ----------------------------------
in this program which is the first one constructor , add here with and without no relationship, but if you do not add the second , then that would definitely be wrong , because you are using a parent class method
------ For reference only ------ ---------------------------------
try it yourself know ~
sorry, I mean to delete super.paint (g); phrase
I just want to know , you can add or not to add , why add ? Is there any tips
------ For reference only --------------------------------- ------
I just want to know , you can add or not to add , why add ? Is there any tips
------ For reference only --------------------------------- ------
try it yourself know ~
sorry, I mean to delete super.paint (g); phrase
I just want to know , you can add or not to add , why add ? Is there any tips
That increase will not do,
because even without super, the constructor will be executed when the first implementation of the default constructor of the parent class .
------ For reference only -------------------------------------- -
plus super is called the father of the corresponding method , is completely rewritten without this method
------ For reference only --------------- ------------------------
me giddy . We soon learned ,
------ For reference only ------------------------------- --------
super () function : Create a subclass object created when a parent object , super () is equivalent to calling the default constructor of the parent class , you do not add a constructor execution it will let you create a parent class object
------ For reference only ------------------------- --------------
true, but in this code inside, did not see it added to what is the meaning
------ For reference only ---------------------------------------
true, but in this code inside, did not see it added to what is the meaning
super.paint (g); That is because the parent class 's paint () has no effect subclass
------ For reference only ----- ----------------------------------
true, but in this code inside , did not see it added to what is the meaning
super.paint (g); That is because the parent class 's paint () has no effect subclass
+1
没有评论:
发表评论