📄 zuoye5.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.lang.String.*;
class Father
{
String sname;
float height,weight;
Father(String sname)
{
this.sname=sname;
}
public String getName()
{
return sname;
}
public String speak(String s)
{
String talk=new String(s);
return talk;
}
}
class Son extends Father
{
public Son(String s)
{
super(s);
}
}//继承
class ChengChe
{
String s1,s2,s3;
int a;
ChengChe()
{
s1=new String("我骑自行车去天安门。");
}
ChengChe(String s)
{
this.s2=s;
}
ChengChe(int i,String s)
{
a=i;
s3=s;
}
}//重写
class SuperClass
{
String str,str1,str2;
int i;
SuperClass(String str)
{
this.str=str;
}
}
class SubClass
{
String str,str1,str2;
int i;
SubClass(String str1)
{
this.str1=str1;
}
SubClass(int i,String str2)
{
this.i=i;
this.str2=str2;
}
}//构造函数
class Windows extends Frame implements ActionListener
{
Button button1,button2,button3,button4;
TextArea area;Label lab;
Windows(String s)
{
super(s);
setLayout(new FlowLayout());
lab=new Label("操作选择:",Label.CENTER);
lab.setBackground(new Color(0,250,250));
button1=new Button("继承");
button1.setBackground(Color.blue);
button1.setForeground(Color.cyan);
button1.addActionListener(this);
button2=new Button("重载");
button2.setBackground(Color.blue);
button2.setForeground(Color.cyan);
button2.addActionListener(this);
button3=new Button("构造函数的继承与重载");
button3.setBackground(Color.blue);
button3.setForeground(Color.cyan);
button3.addActionListener(this);
add(lab);
add(button1);
add(button2);
add(button3);
area=new TextArea();
add(area);
button4=new Button("关闭");
button4.setBackground(Color.red);
button4.setForeground(Color.yellow);
add(button4);
button4.addActionListener(this);
setBounds(100,100,500,500);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
{
area.setText(null);
area.setFont(new Font("黑体",Font.BOLD,15));
area.setBackground(new Color(151,80,50));
Father father=new Father("Father");
Son son=new Son("son");
father.height=180.0f;
father.weight=160f;
son.height=160.0f;
son.weight=120.0f;
area.append(""+father.speak("I'm father..")+"\n");
area.append("\nMy height is "+father.height+" and my weight is"+father.weight+"\n");
area.append("\n"+son.speak("I'm son..")+"\n");
area.append("\nMy height is "+son.height+" and my weight is"+son.weight+"\n");
}
else if(e.getSource()==button2)
{
area.setText(null);
area.setFont(new Font("楷书",Font.ITALIC,15));
area.setBackground(new Color(50,180,100));
ChengChe 乘车1=new ChengChe();
area.append(乘车1.s1+"\n");
ChengChe 乘车2=new ChengChe("摩托车");
area.append("\n我骑"+乘车2.s2+"去天安门。\n");
ChengChe 乘车3=new ChengChe(8,"乘公交车");
area.append("\n我早上"+乘车3.a+"点"+乘车3.s3+"去天安门。\n");
}
else if(e.getSource()==button3)
{
area.setText(null);
area.setFont(new Font("幼圆",Font.ROMAN_BASELINE,15));
area.setBackground(new Color(46,100,250));
SuperClass sup=new SuperClass("我是父类构造函数。");
area.append(sup.str+"\n");
SubClass sub1=new SubClass("我是子类的第一个构造函数。");
area.append("\n"+sub1.str1+"\n");
SubClass sub2=new SubClass(1,"我是子类的第二个构造函数。");
area.append("\n"+sub2.str2);
}
else if(e.getSource()==button4)
{
System.exit(0);
}
}
}
public class zuoye5
{
public static void main(String args[])
{
Windows win=new Windows("第五次Java实验");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -