以1000行為準,超過千行就要考慮類拆分了。
成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供平陸企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計、做網(wǎng)站、H5技術(shù)、小程序制作等業(yè)務(wù)。10年已為平陸眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進行中。
對于經(jīng)常使用的java類,代碼行數(shù)應(yīng)該盡可能的少,這樣能減少java類的加載時間,減少內(nèi)存頻繁占用和回收。如果類過大,java類加載會耗時并且占用內(nèi)存大。容易引起內(nèi)存回收。
如果前期準備得當,設(shè)計,建模合理。一般來說,每個類的代碼就不會很多,幾百行。很早之前看過有前輩說,300行之內(nèi)。
/**
* 文件名:Operation.java 2014-1-2 下午3:06:37
* @author Administrator
*/
package cc.icoc.javaxu.action;
import java.util.ArrayList;
import java.util.Scanner;
import cc.icoc.javaxu.datas.StudentInfo;
/**
* @author 許仕永
* 創(chuàng)建時間: 2014 2014-1-2 下午3:06:37
*/
public class Operation
{
int stuNo,stuAge;
String stuName,stuClass,stuSex;
ArrayListStudentInfo list;
/**程序入口
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
new Operation().operation();
}
private int inputInt()
{
Scanner scanner = new Scanner(System.in);
return scanner.nextInt();
}
private String inputStr()
{
Scanner scanner = new Scanner(System.in);
return scanner.nextLine();
}
private void operation()
{
// TODO Auto-generated method stub
if(list == null)
{
list = new ArrayListStudentInfo();
}
while(true)
{
System.out.println("\t\t\t學(xué)生信息管理系統(tǒng)\n");
System.out.println("1.信息查閱");
System.out.println("2.信息查詢");
System.out.println("3.信息錄入");
System.out.println("4.退出系統(tǒng)");
int what = inputInt();
if ( what == 1 )
{
getInfo();
} else if ( what == 2 )
{
allSerach();
} else if ( what == 3 )
{
inputInfo();
} else if( what == 4 )
{
System.out.println("已經(jīng)退出系統(tǒng)");
System.exit(0);
}
}
}
/**
* 獲取全部學(xué)生的信息列表
*/
private void getInfo()
{
if(!list.isEmpty())
{
for ( int i = 0; i list.size(); i++ )
{
System.out.println(list.get(i));
}
}
}
/**
* 執(zhí)行查找
*/
private void allSerach()
{
System.out.println("1.輸入學(xué)號進行查詢");
System.out.println("2.輸入姓名進行查詢");
int what = inputInt();
if(what == 1)
{
System.out.println("請輸入學(xué)號:");
search(inputInt());
}
else
{
System.out.println("請輸入姓名:");
search(inputStr());
}
}
/**
* 按學(xué)生姓名查詢學(xué)生信息
* @param name 學(xué)生姓名
*/
private StudentInfo search(String name)
{
for ( int i = 0; i list.size(); i++ )
{
if(list.get(i).getStuName().equals(name))
{
System.out.println(list.get(i));
return list.get(i);
}
}
System.out.println("未查詢到該名字的學(xué)生");
return null;
}
/**
* 按學(xué)號查詢學(xué)生信息
* @param id 學(xué)號
*/
private StudentInfo search(int id)
{
for ( int i = 0; i list.size(); i++ )
{
if(list.get(i).getStuNo()==id)
{
System.out.println(list.get(i));
return list.get(i);
}
}
System.out.println("未查詢到該學(xué)號的學(xué)生");
return null;
}
/**
* 錄入學(xué)生信息
*/
private void inputInfo()
{
try
{
System.out.print("請輸入學(xué)生學(xué)號:\n");
stuNo = inputInt();
System.out.print("請輸入學(xué)生姓名:\n");
stuName = inputStr();
System.out.print("請輸入學(xué)生性別:\n");
stuSex = inputStr();
System.out.print("請輸入學(xué)生年齡:\n");
stuAge = inputInt();
System.out.print("請輸入學(xué)生班級:\n");
stuClass = inputStr();
list.add(new StudentInfo(stuNo, stuAge, stuName, stuSex, stuClass));
} catch (Exception e)
{
// TODO: handle exception
System.out.println("不允許為空,請重新輸入");
}
}
}
package cc.icoc.javaxu.action;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Stu
{
int countStu = 0;
int sum = 0;
int avg = 0;
ListStuBean list = new ArrayListStu.StuBean();
public static void main(String[] args)
{
new Stu().go();
}
private void go()
{
A:while(true)
{
//構(gòu)造實體Bean來存儲學(xué)生成績
StuBean bean = new StuBean();
System.out.println("請輸入成績:");
System.out.println("語文:");
bean.setYuwen(scanner());
System.out.println("數(shù)學(xué):");
bean.setMath(scanner());
//獲取英語成績
System.out.println("英語:");
bean.setEnglish(scanner());
//存入容器
list.add(bean);
for ( int i = 0; i list.size(); i++ )
{
sum += list.get(i).english+list.get(i).math+list.get(i).yuwen;
}
avg = sum / (3*list.size());
System.out.println("當前有"+list.size()+"條學(xué)生記錄,所有學(xué)生總分="+sum+"平均分為"+avg);
//輸入y繼續(xù)錄入學(xué)生信息,輸入x退出
System.out.println("\n已成功錄入一個學(xué)生的數(shù)據(jù),是否繼續(xù)/退出(y/x)");
if("x".equals(scannerString()))
break A;
}
}
/**輸入*/
private int scanner()
{
Scanner scanner = new Scanner(System.in);
return scanner.nextInt();
}
/**輸入*/
private String scannerString()
{
Scanner scanner = new Scanner(System.in);
return scanner.nextLine();
}
class StuBean
{
int math;
int yuwen;
int english;
public int getMath()
{
return math;
}
public void setMath(int math)
{
this.math = math;
}
public int getYuwen()
{
return yuwen;
}
public void setYuwen(int yuwen)
{
this.yuwen = yuwen;
}
public int getEnglish()
{
return english;
}
public void setEnglish(int english)
{
this.english = english;
}
}
}
/**
* 文件名:StudentInfo.java 2014-1-2 下午3:07:29
* @author Administrator
*/
package cc.icoc.javaxu.datas;
/**
* @author 許仕永
* 創(chuàng)建時間: 2014 2014-1-2 下午3:07:29
*/
public class StudentInfo
{
int stuNo,stuAge;
String stuName,stuSex,stuClass;
public StudentInfo(int stuNo, int stuAge, String stuName, String stuSex, String stuClass)
{
super();
this.stuNo = stuNo;
this.stuAge = stuAge;
this.stuName = stuName;
this.stuSex = stuSex;
this.stuClass = stuClass;
}
public int getStuNo()
{
return stuNo;
}
public void setStuNo(int stuNo)
{
this.stuNo = stuNo;
}
public int getStuAge()
{
return stuAge;
}
public void setStuAge(int stuAge)
{
this.stuAge = stuAge;
}
public String getStuName()
{
return stuName;
}
public void setStuName(String stuName)
{
this.stuName = stuName;
}
public String getStuSex()
{
return stuSex;
}
public void setStuSex(String stuSex)
{
this.stuSex = stuSex;
}
public String getStuClass()
{
return stuClass;
}
public void setStuClass(String stuClass)
{
this.stuClass = stuClass;
}
@Override
public String toString()
{
// TODO Auto-generated method stub
String s = "學(xué)號:"+getStuNo()+"\t姓名:"+getStuName()+" \t性別:"+getStuSex()+"\t年齡:"+getStuAge()+"\t班級:"+getStuClass();
return s;
}
}
具體代碼如下:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Calculator ?extends JFrame implements ActionListener ?{
private JFrame jf;
private JButton[] allButtons;
private JButton clearButton;
private JTextField jtf;
public Calculator() {
//對圖形組件實例化
jf=new JFrame("任靜的計算器1.0:JAVA版");
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(){
System.exit(0);
}
});
allButtons=new JButton[16];
clearButton=new JButton("清除");
jtf=new JTextField(25);
jtf.setEditable(false);
String str="123+456-789*0.=/";
for(int i=0;iallButtons.length;i++){
allButtons[i]=new JButton(str.substring(i,i+1));
}
}
public void init(){
//完成布局
jf.setLayout(new BorderLayout());
JPanel northPanel=new JPanel();
JPanel centerPanel=new JPanel();
JPanel southPanel=new JPanel();
northPanel.setLayout(new FlowLayout());
centerPanel.setLayout(new GridLayout(4,4));
southPanel.setLayout(new FlowLayout());
northPanel.add(jtf);
for(int i=0;i16;i++){
centerPanel.add(allButtons[i]);
}
southPanel.add(clearButton);
jf.add(northPanel,BorderLayout.NORTH);
jf.add(centerPanel,BorderLayout.CENTER);
jf.add(southPanel,BorderLayout.SOUTH);
addEventHandler();
}
//添加事件監(jiān)聽
public void addEventHandler(){
jtf.addActionListener(this);
for(int i=0;iallButtons.length;i++){
allButtons[i].addActionListener(this);
}
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Calculator.this.jtf.setText("");
}
});
}
//事件處理
public void actionPerformed(ActionEvent e) {
//在這里完成事件處理 ?使計算器可以運行
String action=e.getActionCommand();
if(action=="+"||action=="-"||action=="*"||action=="/"){
}
}
public void setFontAndColor(){
Font f=new Font("宋體",Font.BOLD,24);
jtf.setFont(f);
jtf.setBackground(new Color(0x8f,0xa0,0xfb));
for(int i=0;i16;i++){
allButtons[i].setFont(f);
allButtons[i].setForeground(Color.RED);
}
}
public void showMe(){
init();
setFontAndColor();
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new Calculator().showMe();
}
}