谁能给我举些构造方法的具体运用的典型例子吗?
我看书后怎么觉的构造方法在写代码中都没怎么运用到的呢
我看书后怎么觉的构造方法在写代码中都没怎么运用到的呢
解决方案 »
- 怎样给其中的button1和button2添加监听事件监听器?
- count返回值怎么能获取到?
- -_- 文件输入输出流 删除文件中的部分数据
- 请问 JBuild调试时 怎么出现一个相同文件名的文件,单步不了
- 使用JB2005,在打包jar时,在MANIFEST.MF中指定Main-Class时,"MANIFEST.MF": Error reading manifest: java.io.IOException: invalid he
- 关于单元测试的问题,请大家帮帮忙
- JDK那里出了问题Exception in thread "main" java.lang.NoClassDefFoundError: test
- 大家能 告诉我在APPLET里怎么查找数据库吗
- Smack开发Web即时通信
- 编程参考手册!
- for 跳出两层循环
- 输出 能被3整除 但各位数是6的 100 以内的数 && 求1000以内的水仙花数
static String str ="";
public static void main(String[] args) {
int x = 1, y;
Scanner scan = new Scanner(System.in);
y = scan.nextInt();
digui(x, y);
}
public static int digui(int x, int y){
if(x == y){
return x;
}else{
str += x + "-";
System.out.println(str + y);
return digui(x + 1, y);
}
}
}
public class Bankwork {
double high;
double hight;
string Qualifications;
Bankword(double high,double hight,string Qualifications)
{
this.high=high;
this.hight=hight;
this.Qualifications=Qualifications;
}
Bankword()
{}
}
{
int num;
People(num)
{
this.num=num;
}
}
(1)构造方法名与类名相同.
(2)构造方法不返回任何值,也没有返回类型.
(3)每个类可以有零个或多个构造方法.
(4)构造方法在创建对象时自动执行,一般不能显式地直接调用.
import javax.swing.*; public class LottoMadness extends JFrame{
//创建一个LottoEvent的实例对象lotto
LottoEvent lotto = new LottoEvent(this); //第1排按钮
JPanel row1 = new JPanel();//创建面板1
//建立一个选择框组
//编成一组后组内只能同时选择一个(就是单选的意思 )
ButtonGroup option = new ButtonGroup();
//选择按钮“机选”默认不选中
JCheckBox quickpick = new JCheckBox("机选",false);
//选择按钮“自选” 默认选中
JCheckBox personal = new JCheckBox("自选",true);
//第2排按钮
JPanel row2 = new JPanel();//创建面板2
//标签“你的号码:”并且右对齐
JLabel numbersLabel = new JLabel("你的号码:",JLabel.RIGHT);
//一组文本框(6个)
JTextField[] numbers = new JTextField[6];
//标签“ 中奖号码:”并且右对齐
JLabel winnersLabel = new JLabel("中奖号码:",JLabel.RIGHT);
//一组文本框(6个)
JTextField[] winners = new JTextField[6];
//第3排按钮
JPanel row3 = new JPanel();//创建面板3
//按钮“停止”
JButton stop = new JButton("停止");
//按钮“开始”
JButton play = new JButton("开始");
//按钮“重置”
JButton reset = new JButton("重置");
//第4排按钮
JPanel row4 = new JPanel();//创建面板4
//标签“ 3 of 6: ”并且右对齐
JLabel got3Label = new JLabel("六中三: " , JLabel.RIGHT);
//文本框 默认0
JTextField got3 = new JTextField("0");
//标签“ 4 of 6: ”并且右对齐
JLabel got4Label = new JLabel("六中四: " , JLabel.RIGHT);
//文本框 默认0
JTextField got4 = new JTextField("0");
//标签“ 5 of 6: ”并且右对齐
JLabel got5Label = new JLabel("六中五: " , JLabel.RIGHT);
//文本框 默认0
JTextField got5 = new JTextField("0");
//标签 “6 of 6: ”并且右对齐
JLabel got6Label = new JLabel("全中: " , JLabel.RIGHT);
//文本框 默认0
JTextField got6 = new JTextField("0",10);
//标签“ Drawing: ”并且右对齐
JLabel drawingsLabel = new JLabel("次数: " , JLabel.RIGHT);
//文本框 默认0
JTextField drawings = new JTextField("0");
//标签“ Years: ”并且右对齐
JLabel yearsLabel = new JLabel("时间(年): " , JLabel.RIGHT);
//文本框 默认0
JTextField years = new JTextField("0");
//重写LottoMadness的构造函数
public LottoMadness(){
//标题 六合彩
super("六合彩");
//窗口大小
setSize(550,270);
//点击窗口上的X的动作 就是关闭~囧
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//创建GridLayout类型的布局管理器layout
//指定它的行数为5 列数为1水平间距10垂直间距10
GridLayout layout = new GridLayout(5,1,10,10);
setLayout(layout);
//监听quickpick
quickpick.addItemListener(lotto);
//监听personal
personal.addItemListener(lotto);
//监听stop
stop.addActionListener(lotto);
//监听play
play.addActionListener(lotto);
//监听reset
reset.addActionListener(lotto);
//创建FlowLayout类型的布局管理器layout1
//让它居中对齐并指定的水平和垂直间隙为10
FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER,10,10);
//添加按钮quickpick到option组
option.add(quickpick);
//添加按钮personal到option组
option.add(personal);
//设定row1的布局管理器为layout1
row1.setLayout(layout1);
//添加按钮quickpick到面板1
row1.add(quickpick);
//添加按钮personal到面板1
row1.add(personal);
//加入面板1
add(row1);
//创建GridLayout类型的布局管理器layout2
//指定它的行数为2 列数为7水平间距10垂直间距10
GridLayout layout2 = new GridLayout(2,7,10,10);
//设定row2的布局管理器为layout2
row2.setLayout(layout2);
//添加标签numbersLabel到面板2
row2.add(numbersLabel);
//循环 将数字分别放入numbers文本框组中
for(int i = 0; i < 6; i++){
numbers[i] = new JTextField();
row2.add(numbers[i]);
}
//添加标签winnersLabel到面板2
row2.add(winnersLabel);
//循环 将数字分别放入winners文本框组中
for(int i = 0; i < 6; i++){
winners[i] = new JTextField();
//禁用winners文本框(因为要用来显示)
winners[i].setEditable(false);
row2.add(winners[i]);
}
//加入面板2
add(row2);
//创建FlowLayout类型的布局管理器layout3
//让它居中对齐并指定的水平和垂直间隙为10
FlowLayout layout3 = new FlowLayout(FlowLayout.CENTER,10,10);
//设定row3的布局管理器为layout3
row3.setLayout(layout3);
//按钮 “停止” 默认 禁用
stop.setEnabled(false);
//添加按钮stop到面板3
row3.add(stop);
//添加按钮play到面板3
row3.add(play);
//添加按钮reset到面板3
row3.add(reset);
//加入面板3
add(row3);
//创建GridLayout类型的布局管理器layout4
//指定它的行数为2 列数为3水平间距20垂直间距10
GridLayout layout4 = new GridLayout(2,3,20,10);
//设定row4的布局管理器为layout4
row4.setLayout(layout4);
//添加标签got3Label到面板4
row4.add(got3Label);
//禁用got3文本框(因为要用来显示)
got3.setEditable(false);
//添加文本框got3到面板4
row4.add(got3);
//添加标签got4Label到面板4
row4.add(got4Label);
//禁用got4文本框(因为要用来显示)
got4.setEditable(false);
//添加文本框got4到面板4
row4.add(got4);
//添加标签got5Label到面板4
row4.add(got5Label);
//禁用got5文本框(因为要用来显示)
got5.setEditable(false);
//添加文本框got5到面板4
row4.add(got5);
//添加标签got6Label到面板4
row4.add(got6Label);
//禁用got6文本框(因为要用来显示)
got6.setEditable(false);
//添加文本框got6到面板4
row4.add(got6);
//添加标签drawingsLabel到面板4
row4.add(drawingsLabel);
//禁用drawings文本框(因为要用来显示)
drawings.setEditable(false);
//添加文本框drawings到面板4
row4.add(drawings);
//添加标签yearsLabel到面板4
row4.add(yearsLabel);
//禁用years文本框(因为要用来显示)
years.setEditable(false);
//添加文本框years到面板4
row4.add(years);
//加入面板4
add(row4);
//显示
setVisible(true);
}
public static void main(String[] args){
//创建一个LottoMadness的实例对象
LottoMadness frame = new LottoMadness();
}
}
import java.awt.event.*; public class LottoEvent
implements ItemListener,ActionListener,Runnable{
//创建一个LottoMadness的对象gui
LottoMadness gui;
//Thread线程playing
Thread playing;
//重写LottoEvent的构造函数
public LottoEvent(LottoMadness in){
//将in的值赋予gui
gui = in;
}
public void actionPerformed(ActionEvent event){
//将按下的按钮传递给command并来判断 以执行各自的方法
String command = event.getActionCommand();
if(command == "开始"){
startPlaying();
}
if(command == "停止"){
stopPlaying();
}
if(command == "重置"){
clearAllFields();
}
}
//"开始"按钮的方法
void startPlaying() {
//新建一个线程
playing = new Thread(this);
//启动这个线程
playing.start();
//除了"停止"按钮外其他禁用
gui.play.setEnabled(false);
gui.stop.setEnabled(true);
gui.reset.setEnabled(false);
gui.quickpick.setEnabled(false);
gui.personal.setEnabled(false);
}
//"停止"按钮的方法
void stopPlaying() {
//除了"停止"按钮外其他开启
gui.stop.setEnabled(false);
gui.play.setEnabled(true);
gui.reset.setEnabled(true);
gui.quickpick.setEnabled(true);
gui.personal.setEnabled(true);
//将线程清空等待回收
playing = null;
}
//"重置"按钮的方法
void clearAllFields() {
//用循环把第二排文本框的数字都清空
for(int i = 0; i < 6; i++){
gui.numbers[i].setText(null);
gui.winners[i].setText(null);
}
//把第四排文本框的数字都清空
gui.got3.setText("0");
gui.got4.setText("0");
gui.got5.setText("0");
gui.got6.setText("0");
gui.drawings.setText("0");
gui.years.setText("0");
}
public void itemStateChanged(ItemEvent event){
//event.getItem返回一个对象
Object item = event.getItem();
//当返回的是gui.quickpick方法时候
if(item == gui.quickpick){
//生成6个1到50的随机数字
for(int i = 0; i < 6; i++){
int pick;
do{
pick = (int)Math.floor(Math.random() * 50 + 1);
}while(numberGone(pick,gui.numbers,i));
gui.numbers[i].setText("" + pick);
}
//当返回的不是gui.quickpick方法时候
}else{
//清空文本框
for(int i = 0; i < 6; i++){
gui.numbers[i].setText(null);
}
}
}
//将文本框中内容转换成int 加1后再转化成字符串
void addOneToField(JTextField field){
int num = Integer.parseInt("0" + field.getText());
num++;
field.setText("" + num);
}
//是中奖号码的每个数字都不相等
boolean numberGone(int num,JTextField[] pastNums,int count){
for(int i = 0; i < count; i++){
if(Integer.parseInt(pastNums[i].getText()) == num){
return true;
}
}
return false;
}
//检查数字是否中奖
boolean matchedOne(JTextField win,JTextField[] allPicks){
for(int i = 0; i < 6; i++){
String winText = win.getText();
if(winText.equals(allPicks[i].getText())){
return true;
}
}
return false;
}
public void run(){
Thread thisThread = Thread.currentThread();
while(playing == thisThread){
addOneToField(gui.drawings);
int draw = Integer.parseInt(gui.drawings.getText());
float numYears = (float)draw / 104;
gui.years.setText("" + numYears);
int matches = 0;
for(int i = 0; i < 6;i++){
int ball;
do{
//得到一个1到50的随机数字
ball = (int)Math.floor(Math.random() * 50 + 1);
}while(numberGone(ball,gui.winners,i));
gui.winners[i].setText("" + ball);
if(matchedOne(gui.winners[i],gui.numbers)){
matches++;
}
}
//判断中了几个号码
switch(matches){
case 3:
addOneToField(gui.got3);
break;
case 4:
addOneToField(gui.got4);
break;
case 5:
addOneToField(gui.got5);
break;
case 6:
addOneToField(gui.got6);
gui.stop.setEnabled(false);
gui.play.setEnabled(true);
playing = null;
}
try{
Thread.sleep(100);
}catch(InterruptedException e){
//
}
}
}
}
{
c1(int i)
{
System.out.println("构造方法"+i);
}
c1()
{
System.out.println("构造方法");
}
}
class c2
{
public static void main(String[] args)
{ int i=1;
new c1(i);
new c1();
}
}