import java.awt.*;
創(chuàng)新互聯(lián)公司是一家專注于網(wǎng)站設(shè)計(jì)制作、網(wǎng)站制作與策劃設(shè)計(jì),元謀網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)10年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:元謀等地區(qū)。元謀做網(wǎng)站價(jià)格咨詢:18980820575
import java.awt.event.*;
import java.util.Random;
import javax.swing.Timer;
public class PinBall
{
private final int TABLE_WIDTH = 300;//桌面寬度
private final int TABLE_HEIGHT = 400;//桌面高度
private final int RACKET_Y = 340;//球拍的垂直位置
private final int RACKET_HEIGHT = 20;//球拍高度
private final int RACKET_WIDTH = 60;//球拍寬度
private final int BALL_SIZE = 16;//球的大小
private Frame f = new Frame("彈球游戲");//實(shí)例化一個(gè)窗口
Random rand = new Random();//實(shí)例化一個(gè)隨機(jī)數(shù)生成器
private int ySpeed = 10;//小球的縱向運(yùn)動(dòng)數(shù)度、
private double xyRate = rand.nextDouble() - 0.5;//返回一個(gè)-0.5到0.5之間的比率用控制小球運(yùn)動(dòng)方向
private int xSpeed = (int)(ySpeed*xyRate*2);//這個(gè)橫向速度在-10到10之間,產(chǎn)生左右擺動(dòng)運(yùn)動(dòng)效果
private int ballX = rand.nextInt(200)+20;//小球開始的橫坐標(biāo)位置,200表示產(chǎn)生0到100之間的隨機(jī)數(shù)
private int ballY = rand.nextInt(10)+20;//小球開始的縱坐標(biāo)位置
private int racketX = rand.nextInt(200);//球拍開始時(shí)的橫坐標(biāo)位置
private MyCanvas tableArea = new MyCanvas();//實(shí)力化一個(gè)畫布工具,集成Canvas類
private String shape = "";//保存需要繪制圖形的字符串屬性
Timer timer;//聲明一個(gè)時(shí)間變量
private boolean isLose = false;//表示游戲是否結(jié)束
public void init()
{
tableArea.setPreferredSize(new Dimension(TABLE_WIDTH,TABLE_HEIGHT));//定義畫布大小
f.add(tableArea);//添加畫布到窗口
KeyAdapter keyProcessor = new KeyAdapter()//實(shí)例化一個(gè)鍵盤監(jiān)聽事件適配器
{
public void keyPressed(KeyEvent ke)//重寫適配器里面的按下某鍵盤方法
{
if(ke.getKeyCode()==KeyEvent.VK_LEFT)//按下鍵盤左鍵時(shí)
{
if(racketX 0)//球拍左邊框不能出畫布的左邊框
racketX -=10;//按一左鍵次向左移動(dòng)10個(gè)像素
}
if(ke.getKeyCode()==KeyEvent.VK_RIGHT)//按下鍵盤右鍵時(shí)
{
if(racketX TABLE_WIDTH - RACKET_WIDTH)//球拍右邊框不能出畫布的右邊框
racketX +=10;//按一次右鍵移動(dòng)向右移動(dòng)10個(gè)像素
}
}
};
f.addKeyListener(keyProcessor);//給窗口添加鍵盤監(jiān)聽器
tableArea.addKeyListener(keyProcessor);//給畫布添加鍵盤監(jiān)聽器
ActionListener taskPerformer = new ActionListener()//這里是實(shí)例化了一個(gè)監(jiān)聽接口,這個(gè)接口里面只有一個(gè)方法
{
public void actionPerformed(ActionEvent evt)//重寫這個(gè)接口里面的方法,判斷小球的位置
{
if(ballX=0 || ballX=TABLE_WIDTH-BALL_SIZE)//保證小球橫向上在畫布之內(nèi)運(yùn)動(dòng)
{
xSpeed = -xSpeed;//觸發(fā)反方向運(yùn)動(dòng)
}
if(ballY=RACKET_Y-BALL_SIZE(ballXracketX||ballXracketX+RACKET_WIDTH))//出了球拍的可擊打范圍
{
timer.stop();//停止對(duì)監(jiān)聽器的觸發(fā)
isLose=true;//將標(biāo)志isLose變量置為true
tableArea.repaint();//調(diào)用畫布的重繪方法
}
else if(ballY=0||(ballY=RACKET_Y-BALL_SIZEballYracketXballX=racketX+RACKET_WIDTH))//小球在球拍之內(nèi),而其到達(dá)球拍的高度
{
ySpeed=-ySpeed;//上下方向改變,小球反彈
}
ballY+=ySpeed;//小球的坐標(biāo)在縱向上增加
ballX+=xSpeed;//小球的坐標(biāo)在橫向上的增加
tableArea.repaint();//調(diào)用畫布的重繪方法3
}
};
timer = new Timer(100,taskPerformer);//每隔0.1秒運(yùn)行一次監(jiān)聽器
timer.start();//計(jì)時(shí)器開始運(yùn)行
f.addWindowListener(new MyListener());//關(guān)閉窗口事件
f.pack();//設(shè)置窗口最佳大小
f.setVisible(true);//顯示窗口
}
class MyListener extends WindowAdapter//關(guān)閉窗口的類
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
public static void main(String[] args)//程序入口
{
new PinBall().init();//調(diào)用PinBall類里面的init()方法
}
class MyCanvas extends Canvas//建一個(gè)集成Canvas類的類
{
public void paint(Graphics g)//重寫父類的繪圖方法
{
if(isLose)//如果isLose為真,則在畫布里打印“游戲已結(jié)束”
{
g.setColor(new Color(255,0,0));//當(dāng)前顏色
g.setFont(new Font("黑體",Font.BOLD,30));//字體名稱,樣式,大小
g.drawString("游戲已結(jié)束!",50,200);//按坐標(biāo)繪制文字圖形
}
else//負(fù)責(zé)
{
g.setColor(new Color(240,240,80));//當(dāng)前顏色
g.fillOval(ballX,ballY,BALL_SIZE,BALL_SIZE);//填充顏色,根據(jù)坐標(biāo)和長寬填充圓形
g.setColor(new Color(80,80,200));//當(dāng)前顏色
g.fillRect(racketX,RACKET_Y,RACKET_WIDTH,RACKET_HEIGHT);//填充顏色,根據(jù)坐標(biāo)和長寬填充矩形
}
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ConstructFrame extends JFrame
{
private static final long serialVersionUID = 1L;
String value1="",result,value2="";
int flag=0,fix=0,sum=1;
Boolean happy;
JTextField text=new JTextField(30);
int flagsum=0;
Container c=getContentPane();
JButton buttonx;
ConstructFrame()
{ super("計(jì)算器");
c.setLayout(null);
c.setBackground(Color.blue);
this.setSize(400, 400);
c.add(text);
text.setHorizontalAlignment(JTextField.RIGHT);
final JButton buttonx=new JButton("BackSpace");
c.add(buttonx);
buttonx.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
int count=0;
String temp;
if(flag==0)
{
count=value1.length();
if(count!=1)
temp=value1.substring(0, count-1);
else
temp="0";
value1=temp;
}
else
{
count=value2.length();
if(count!=1)
temp=value2.substring(0, count-1);
else
temp="0";
value2=temp;
}
text.setText(temp);
}
});
final JButton buttony=new JButton("CE");
c.add(buttony);
buttony.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
value1="";
value2="";
flag=0;
text.setText("0");
}
});
final JButton button1=new JButton("1");
c.add(button1);
button1.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+1;
temp=value1;
}
else
{
value2=value2+1;
temp=value2;
}
text.setText(temp);
}
});
final JButton button2=new JButton(" 2 ");
c.add(button2);
button2.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+2;
temp=value1;
}
else
{
value2=value2+2;
temp=value2;
}
text.setText(temp);
}
});
final JButton button3=new JButton("3");
c.add(button3);
button3.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+3;
temp=value1;
}
else
{
value2=value2+3;
temp=value2;
}
text.setText(temp);
}
});
final JButton button4=new JButton("4");
c.add(button4);
button4.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+4;
temp=value1;
}
else
{
value2=value2+4;
temp=value2;
}
text.setText(temp);
}
});
final JButton button5=new JButton("5");
c.add(button5);
button5.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+5;
temp=value1;
}
else
{
value2=value2+5;
temp=value2;
}
text.setText(temp);
}
});
final JButton button6=new JButton("6");
c.add(button6);
button6.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+6;
temp=value1;
}
else
{
value2=value2+6;
temp=value2;
}
text.setText(temp);
}
});
final JButton button7=new JButton("7");
c.add(button7);
button7.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+7;
temp=value1;
}
else
{
value2=value2+7;
temp=value2;
}
text.setText(temp);
}
});
final JButton button8=new JButton("8");
c.add(button8);
button8.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+8;
temp=value1;
}
else
{
value2=value2+8;
temp=value2;
}
text.setText(temp);
}
});
final JButton button9=new JButton("9");
c.add(button9);
button9.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+9;
temp=value1;
}
else
{
value2=value2+9;
temp=value2;
}
text.setText(temp);
}
});
final JButton button0=new JButton("0");
c.add(button0);
button0.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
String temp;
if(flag==0)
{
value1=value1+0;
temp=value1;
}
else
{
value2=value2+0;
temp=value2;
}
text.setText(temp);
}
});
final JButton buttonadd=new JButton(" + ");
c.add(buttonadd);
buttonadd.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
flag=1;
fix=1;
flagsum=0;
}
});
final JButton buttonsubtract=new JButton(" - ");
c.add(buttonsubtract);
buttonsubtract.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
flag=1;
fix=2;
flagsum=0;
}
});
final JButton buttoncheng=new JButton(" * ");
c.add(buttoncheng);
buttoncheng.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
flag=1;
fix=3;
flagsum=0;
}
});
final JButton buttonchu=new JButton(" / ");
c.add(buttonchu);
buttonchu.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
flag=1;
flagsum=0;
fix=4;
}
});
final JButton buttonequal=new JButton(" = ");
c.add(buttonequal);
buttonequal.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
double temp1,temp2;
double temp=0;
flagsum=0;
temp1=Double.parseDouble(value1);
temp2=Double.parseDouble(value2);
flag=0;
switch(fix)
{
case 1: temp=temp1+temp2;break;
case 2: temp=temp1-temp2;break;
case 3: temp=temp1*temp2;break;
case 4: temp=temp1/temp2;break;
}
result=Double.valueOf(temp).toString();
value1=result;
value2="";
flag=1;
text.setText(result);
}
});
final JButton buttonpoint=new JButton(".");
c.add(buttonpoint);
buttonpoint.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{ if(flagsum==0)
{
String temp;
if(flag==0 )
{
value1=value1+".";
temp=value1;
}
else
{
value2=value2+".";
temp=value2;
}
flagsum=1;
text.setText(temp);
}
}
});
JButton buttonz=new JButton("Start");
c.add(buttonz);
buttonz.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{ if(sum%2==1)
{
happy=true;
text.setText("0.");
flag=0;
}
else
{
happy=false;
value1="";
value2="";
text.setText("");
}
text.setEnabled(happy);
button1.setEnabled(happy);
button2.setEnabled(happy);
button3.setEnabled(happy);
button4.setEnabled(happy);
button5.setEnabled(happy);
button6.setEnabled(happy);
button7.setEnabled(happy);
button8.setEnabled(happy);
button9.setEnabled(happy);
button0.setEnabled(happy);
buttonx.setEnabled(happy);
buttony.setEnabled(happy);
buttonadd.setEnabled(happy);
buttonsubtract.setEnabled(happy);
buttonpoint.setEnabled(happy);
buttonequal.setEnabled(happy);
buttoncheng.setEnabled(happy);
buttonchu.setEnabled(happy);
sum++;
}
});
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
button4.setEnabled(false);
button5.setEnabled(false);
button6.setEnabled(false);
button7.setEnabled(false);
button8.setEnabled(false);
button9.setEnabled(false);
button0.setEnabled(false);
buttonx.setEnabled(false);
buttony.setEnabled(false);
buttonadd.setEnabled(false);
buttonsubtract.setEnabled(false);
buttonpoint.setEnabled(false);
buttonequal.setEnabled(false);
buttoncheng.setEnabled(false);
buttonchu.setEnabled(false);
text.setEnabled(false);
text.setBounds(20, 20, 200, 40);
buttonx.setBounds(20, 60,100, 40);
buttony.setBounds(140, 60,100, 40);
buttonz.setBounds(260, 60,80, 40);
button1.setBounds(20, 120,60, 40);
button2.setBounds(100, 120,60, 40);
button3.setBounds(180, 120,60, 40);
buttonadd.setBounds(260, 120,60, 40);
button4.setBounds(20, 180,60, 40);
button5.setBounds(100, 180,60, 40);
button6.setBounds(180, 180,60, 40);
buttonsubtract.setBounds(260, 180,60, 40);
button7.setBounds(20, 240,60, 40);
button8.setBounds(100, 240,60, 40);
button9.setBounds(180, 240,60, 40);
buttoncheng.setBounds(260,240,60,40);
button0.setBounds(20, 300,60, 40);
buttonpoint.setBounds(100, 300, 60, 40);
buttonequal.setBounds(180,300,60, 40);
buttonchu.setBounds(260, 300,60, 40);
setVisible(true);
}
class MYMouseEvent extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
value1=e.toString();
text.setText(value1);
}
}
}
class Calutator
{
public static void main(String[] args)
{
new ConstructFrame();
}
}
你自己慢慢的看吧!
上面 wuzhikun12同學(xué)寫的不錯(cuò),但我想還不能運(yùn)行,并且還不太完善。我給個(gè)能運(yùn)行的:(注意:文件名為:Test.java)
//要實(shí)現(xiàn)對(duì)象間的比較,就必須實(shí)現(xiàn)Comparable接口,它里面有個(gè)compareTo方法
//Comparable最好使用泛型,這樣,無論是速度還是代碼量都會(huì)減少
@SuppressWarnings("unchecked")
class Student implements ComparableStudent{
private String studentNo; //學(xué)號(hào)
private String studentName; //姓名
private double englishScore; //英語成績
private double computerScore; //計(jì)算機(jī)成績
private double mathScore; //數(shù)學(xué)成績
private double totalScore; //總成績
//空構(gòu)造函數(shù)
public Student() {}
//構(gòu)造函數(shù)
public Student(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
this.studentNo = studentNo;
this.studentName = studentName;
this.englishScore = englishSocre;
this.computerScore = computerScore;
this.mathScore = mathScore;
}
//計(jì)算總成績
public double sum() {
this.totalScore = englishScore+computerScore+mathScore;
return totalScore;
}
//計(jì)算評(píng)測成績
public double testScore() {
return sum()/3;
}
//實(shí)現(xiàn)compareTO方法
@Override
public int compareTo(Student student) {
double studentTotal = student.getTotalScore();
return totalScore==studentTotal?0:(totalScorestudentTotal?1:-1);
}
//重寫toString方法
public String toString(){
return "學(xué)號(hào):"+this.getStudentNo()+" 姓名:"+this.getStudentName()+" 英語成績:"+this.getEnglishScore()+" 數(shù)學(xué)成績:"+this.getMathScore()+" 計(jì)算機(jī)成績:"+this.getComputerScore()+" 總成績:"+this.getTotalScore();
}
//重寫equals方法
public boolean equals(Object obj) {
if(obj == null){
return false;
}
if(!(obj instanceof Student)){
return false;
}
Student student = (Student)obj;
if(this.studentNo.equals(student.getStudentName())) { //照現(xiàn)實(shí)來說,比較是不是同一個(gè)學(xué)生,應(yīng)該只是看他的學(xué)號(hào)是不是相同
return true;
} else {
return false;
}
}
/*以下為get和set方法,我個(gè)人認(rèn)為,totalScore的set的方法沒必要要,因?yàn)樗怯善渌煽冇?jì)算出來的
在set方法中,沒設(shè)置一次值,調(diào)用一次sum方法,即重新計(jì)算總成績
*/
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
sum();
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
sum();
}
public double getEnglishScore() {
return englishScore;
}
public void setEnglishScore(double englishScore) {
this.englishScore = englishScore;
sum();
}
public double getComputerScore() {
return computerScore;
}
public void setComputerScore(double computerScore) {
this.computerScore = computerScore;
sum();
}
public double getMathScore() {
return mathScore;
}
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
sum();
}
public double getTotalScore() {
return totalScore;
}
}
//Student子類學(xué)習(xí)委員類的實(shí)現(xiàn)
class StudentXW extends Student {
//重寫父類Student的testScore()方法
@Override
public double testScore() {
return sum()/3+3;
}
public StudentXW() {}
//StudentXW的構(gòu)造函數(shù)
public StudentXW(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
super(studentNo,studentName,englishSocre,computerScore,mathScore);
}
}
//Student子類班長類的實(shí)現(xiàn)
class StudentBZ extends Student {
//重寫父類Student的testScore()方法
@Override
public double testScore() {
return sum()/3+5;
}
public StudentBZ() {}
//StudentXW的構(gòu)造函數(shù)
public StudentBZ(String studentNo,String studentName,double englishSocre,double computerScore,double mathScore) {
super(studentNo,studentName,englishSocre,computerScore,mathScore);
}
}
//測試類
public class Test {
public static void main(String[] args) {
//生成若干個(gè)student類、StudentXW類、StudentBZ類
Student student1 = new Student("s001","張三",70.5,50,88.5);
Student student2 = new Student("s002","李四",88,65,88.5);
Student student3 = new Student("s003","王五",67,77,90);
StudentXW student4 = new StudentXW("s004","李六",99,88,99.5);
StudentBZ student5 = new StudentBZ("s005","朱漆",56,65.6,43.5);
Student[] students = {student1,student2,student3,student4,student5};
for(int i = 0 ; istudents.length; i++){
double avgScore = students[i].testScore();
System.out.println(students[i].getStudentName()+"學(xué)生的評(píng)測成績?yōu)椋?+ avgScore+"分");
}
}
}
運(yùn)行結(jié)果為:
張三學(xué)生的評(píng)測成績?yōu)椋?9.66666666666667分
李四學(xué)生的評(píng)測成績?yōu)椋?0.5分
王五學(xué)生的評(píng)測成績?yōu)椋?8.0分
李六學(xué)生的評(píng)測成績?yōu)椋?8.5分
朱漆學(xué)生的評(píng)測成績?yōu)椋?0.03333333333333分
我們?cè)谧鲂阅軠y試時(shí),有時(shí)需要自己編寫測試腳本,很多測試工具都支持自定義編寫測試腳本,比如LoadRunner就有很多自定義腳本的協(xié)議,比如"C Vuser","Java Vuser"等協(xié)議.同樣,Jmeter也支持自定義編寫的測試代碼,不過與LoadRunner不同的是,Jmeter沒有自帶編譯器,需要借助第三方編譯器才能實(shí)現(xiàn).下面舉一個(gè)簡單的Java自定義測試代碼例子,使用Java編譯器編寫測試代碼(Java編譯器可以用Eclipse,JBulider等),實(shí)現(xiàn)功能為:在測試前輸入任意一個(gè)字符串,然后判斷該字符串的長度是否大于5,如果大于則測試結(jié)果成功,否則測試結(jié)果位失敗,然后在放到Jmeter中模擬10個(gè)用戶測試,同時(shí)運(yùn)行這段代碼,具體實(shí)現(xiàn)如下:
1.打開Java編譯器,新建一個(gè)項(xiàng)目"TestLength",然后新建一個(gè)包"app".
2.從Jmeter的安裝目錄lib/ext中拷貝兩個(gè)文件"ApacheJMeter_core.jar"和"ApacheJMeter_java.jar"到"Tester"的項(xiàng)目中,然后引入這兩個(gè)JAR文件.(具體的引入方法參考各個(gè)Java編譯器的使用方法)
3.在"app"包中新建一個(gè)類,名字叫"TestLength",不過這個(gè)類要繼承"AbstractJavaSamplerClient"類,如果項(xiàng)目引入步驟二中的兩個(gè)文件,就可以找到"AbstractJavaSamplerClient"類了.
4."TestLength"類在繼承"AbstractJavaSamplerClient"類的同時(shí)也會(huì)繼承四個(gè)方法,分別是"getDefaultParameters","setupTest","runTest"和"teardownTest"方法."getDefaultParameters"方法主要用于設(shè)置傳入的參數(shù);"setupTest"方法為初始化方法,用于初始化性能測試時(shí)的每個(gè)線程."runTest"方法為性能測試時(shí)的線程運(yùn)行體;"teardownTest"方法為測試結(jié)束方法,用于結(jié)束性能測試中的每個(gè)線程.
5.具體實(shí)現(xiàn)代碼如下:
package app;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import com.passpod.core.t8.*;
/**
* @author樂以忘憂
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class TestLength extends AbstractJavaSamplerClient{
private SampleResult results;
private String testStr;
//初始化方法,實(shí)際運(yùn)行時(shí)每個(gè)線程僅執(zhí)行一次,在測試方法運(yùn)行前執(zhí)行,類似于LoadRunner中的init方法
public void setupTest(JavaSamplerContext arg0) {
results = new SampleResult();
testStr = arg0.getParameter("testString", "");
if (testStr != null testStr.length() 0) {
results.setSamplerData(testStr);
}
}
//設(shè)置傳入的參數(shù),可以設(shè)置多個(gè),已設(shè)置的參數(shù)會(huì)顯示到Jmeter的參數(shù)列表中
public Arguments getDefaultParameters() {
Arguments params = new Arguments();
params.addArgument("testStr", ""); //定義一個(gè)參數(shù),顯示到Jmeter的參數(shù)列表中,第一個(gè)參數(shù)為參數(shù)默認(rèn)的顯示名稱,第二個(gè)參數(shù)為默認(rèn)值
return params;
}
//測試執(zhí)行的循環(huán)體,根據(jù)線程數(shù)和循環(huán)次數(shù)的不同可執(zhí)行多次,類似于LoadRunner中的Action方法
public SampleResult runTest(JavaSamplerContext arg0) {
int len = 0;
results.sampleStart(); //定義一個(gè)事務(wù),表示這是事務(wù)的起始點(diǎn),類似于LoadRunner的lr.start_transaction
len = testStr.length();
results.sampleEnd(); //定義一個(gè)事務(wù),表示這是事務(wù)的結(jié)束點(diǎn),類似于LoadRunner的lr.end_transaction
if(len 5){
System.out.println(testStr);
results.setSuccessful(false); //用于設(shè)置運(yùn)行結(jié)果的成功或失敗,如果是"false"則表示結(jié)果失敗,否則則表示成功
}else
results.setSuccessful(true);
return results;
}
//結(jié)束方法,實(shí)際運(yùn)行時(shí)每個(gè)線程僅執(zhí)行一次,在測試方法運(yùn)行結(jié)束后執(zhí)行,類似于LoadRunner中的end方法
public void teardownTest(JavaSamplerContext arg0) {
}
}
6.把上面的例子打包,然后把生成的"TestLength.jar"文件拷貝到Jmeter的安裝目錄lib/ext下.
7.運(yùn)行Jmeter,添加一個(gè)線程組,然后在該線程組下面添加一個(gè)Java請(qǐng)求(在Sampler中),在Java請(qǐng)求的類名稱中選擇咱們剛創(chuàng)建的類"app.TestLength",在下面參數(shù)列表的"testStr"后面輸入要測試的字符串,然后添加一個(gè)監(jiān)聽器(聚合報(bào)告),設(shè)置一下模擬的用戶數(shù)就可以測試了.如果測試不成功,Jmeter會(huì)在它自己個(gè)輸出框中拋出這個(gè)字符串.
通過上面的例子我們可以發(fā)現(xiàn),使用Jmeter自定義Java測試代碼,配合Jmeter自帶的函數(shù),就可以實(shí)現(xiàn)出LoadRunner中"Java Vuser"協(xié)議的絕大多數(shù)功能,而且是沒有用戶數(shù)限制和完全免費(fèi)的(嘿嘿).上面的例子非常簡單,而且沒有任何實(shí)際意義,只是一個(gè)簡單的Jmeter測試代碼示例,用于拋磚引玉,希望大家一起交流,共同 進(jìn)步.
接口自動(dòng)化: 如果是那種http協(xié)議的接口 那么第一種,使用eclipse 自己封裝下httpclient ,然后自己寫java腳本進(jìn)行接口測試 這種要麻煩點(diǎn) 第二種,使用jmeter工具,這個(gè)是專門針對(duì)http接口的進(jìn)行性能以及接口測試工具
本文名稱:java接口自動(dòng)化源代碼,java接口程序代碼
本文來源:http://aaarwkj.com/article18/dssijdp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、標(biāo)簽優(yōu)化、網(wǎng)站設(shè)計(jì)公司、手機(jī)網(wǎng)站建設(shè)、域名注冊(cè)、網(wǎng)頁設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)