JAVA入门级教学之(简单的程序测试)
生活随笔
收集整理的這篇文章主要介紹了
JAVA入门级教学之(简单的程序测试)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
請(qǐng)定義一個(gè)交通工具(Vehicle)類
其中有屬性:
速度speed
體積size
方法移動(dòng)move()
設(shè)置速度setSpeed(int speed)
加速speedUp()
減速speedDown()?
最后在測(cè)試類Vehicle中的main() 中實(shí)例化一個(gè)交通工具對(duì)象,并通過(guò)方法給它初始化speed,size的值并且打印出來(lái),另外調(diào)用加速減速的方法對(duì)速度進(jìn)行改變
?
?
搭建大體框架:
/*** @author LBJ* @version V1.0* @Package PACKAGE_NAME* @date 2021/1/28 17:38* @Copyright 公司*//* 請(qǐng)定義一個(gè)交通工具(Vehicle)類其中有屬性:速度speed體積size方法移動(dòng)move()設(shè)置速度setSpeed(int speed)加速speedUp()減速speedDown()最后在測(cè)試類Vehicle中的main() 中實(shí)例化一個(gè)交通工具對(duì)象,并通過(guò)方法給它初始化speed,size的值并且打印出來(lái),另外調(diào)用加速減速的方法對(duì)速度進(jìn)行改變*/ public class Test01{public static void main(String[] args) {//通過(guò)無(wú)參數(shù)構(gòu)造方法創(chuàng)建對(duì)象Vehicle vehicle = new Vehicle();vehicle.setSpeed(100);vehicle.setSize(20);System.out.println("speed:"+vehicle.getSpeed());System.out.println("size:"+vehicle.getSize());//調(diào)用加速方法vehicle.speedUp();System.out.println(vehicle.getSpeed());//調(diào)用減速方法vehicle.speedDown();System.out.println(vehicle.getSpeed());//通過(guò)有參數(shù)構(gòu)造方法創(chuàng)建對(duì)象//Vehicle vehicle1 = new Vehicle(100, 20);} }class Vehicle {private int speed;private int size;public Vehicle(){}public Vehicle(int speed,int size){this.speed=speed;this.size=size;}public int getSpeed() {return speed;}public void setSpeed(int speed) {this.speed = speed;}public int getSize() {return size;}public void setSize(int size) {this.size = size;}//移動(dòng)方法public void move(){}//加速方法public void speedUp(){}//減速方法噶public void speedDown(){} }結(jié)果:兩個(gè)速度值并沒(méi)有因?yàn)檎{(diào)用方法而改變
?
?
進(jìn)階調(diào)整后的代碼:
/*** @author LBJ* @version V1.0* @Package PACKAGE_NAME* @date 2021/1/28 17:38* @Copyright 公司*//* 請(qǐng)定義一個(gè)交通工具(Vehicle)類其中有屬性:速度speed體積size方法移動(dòng)move()設(shè)置速度setSpeed(int speed)加速speedUp()減速speedDown()最后在測(cè)試類Vehicle中的main() 中實(shí)例化一個(gè)交通工具對(duì)象,并通過(guò)方法給它初始化speed,size的值并且打印出來(lái),另外調(diào)用加速減速的方法對(duì)速度進(jìn)行改變*/ public class Test01{public static void main(String[] args) {//通過(guò)無(wú)參數(shù)構(gòu)造方法創(chuàng)建對(duì)象Vehicle vehicle = new Vehicle();vehicle.setSpeed(100);vehicle.setSize(20);System.out.println("speed:"+vehicle.getSpeed());System.out.println("size:"+vehicle.getSize());//調(diào)用移動(dòng)方法vehicle.move();//調(diào)用加速方法vehicle.speedUp(10);System.out.println("speed:"+vehicle.getSpeed());//調(diào)用減速方法vehicle.speedDown(10);System.out.println("speed:"+vehicle.getSpeed());//通過(guò)有參數(shù)構(gòu)造方法創(chuàng)建對(duì)象//Vehicle vehicle1 = new Vehicle(100, 20);} }class Vehicle {private int speed;private int size;public Vehicle(){}public Vehicle(int speed,int size){this.speed=speed;this.size=size;}public int getSpeed() {return speed;}public void setSpeed(int speed) {this.speed = speed;}public int getSize() {return size;}public void setSize(int size) {this.size = size;}//移動(dòng)方法public void move(){System.out.println("公交車開始啟動(dòng)");}//加速方法public void speedUp(int addSpeed){//this.getSpeed()是原來(lái)的基礎(chǔ)速度,addSpeed是后來(lái)增加的速度this.setSpeed(this.getSpeed() + addSpeed);}//減速方法噶public void speedDown(int subSpeed){if(subSpeed<this.getSpeed()){this.setSpeed(this.getSpeed() - subSpeed);}else {System.out.println("請(qǐng)輸入比當(dāng)前速度小的值");}} }運(yùn)行結(jié)果:
?
編寫簡(jiǎn)單的計(jì)算器
定義名為Number 的類,其中有兩個(gè)整型數(shù)據(jù)成員n1和n2應(yīng)聲明為私有
編寫構(gòu)造方法賦予 n1 和 n2 的初始值
再為該類定義 加、減、乘、除等公有實(shí)例方法
分別對(duì)兩個(gè)成員變量執(zhí)行加減乘除的運(yùn)算
?
大體結(jié)構(gòu):
/*** @author LBJ* @version V1.0* @Package PACKAGE_NAME* @date 2021/1/28 17:38* @Copyright 公司*//* 編寫簡(jiǎn)單的計(jì)算器定義名為Number 的類,其中有兩個(gè)整型數(shù)據(jù)成員n1和n2應(yīng)聲明為私有編寫構(gòu)造方法賦予 n1 和 n2 的初始值再為該類定義 加、減、乘、除等公有實(shí)例方法分別對(duì)兩個(gè)成員變量執(zhí)行加減乘除的運(yùn)算*/ public class Test01{public static void main(String[] args) {Number number = new Number(1,2);number.addition();number.subtration();number.multiplication();number.division();} }class Number{private int n1;private int n2;public Number(){}public Number(int n1,int n2){this.n1=n1;this.n2=n2;}public int getN1() {return n1;}public void setN1(int n1) {this.n1 = n1;}public int getN2() {return n2;}public void setN2(int n2) {this.n2 = n2;}public void addition(){System.out.println(this.getN1()+"+"+this.getN2()+"="+(this.getN1()+this.getN2()));}public void subtration(){System.out.println(this.getN1()+"-"+this.getN2()+"="+(this.getN1()-this.getN2()));}public void multiplication(){System.out.println(this.getN1()+"*"+this.getN2()+"="+(this.getN1()*this.getN2()));}public void division(){System.out.println(this.getN1()+"/"+this.getN2()+"="+(this.getN1()/this.getN2()));} }?
測(cè)試結(jié)果:之所以1/2=0是因?yàn)閕nt類型的取整
?
添加安全控制:
/*** @author LBJ* @version V1.0* @Package PACKAGE_NAME* @date 2021/1/28 17:38* @Copyright 公司*//* 編寫簡(jiǎn)單的計(jì)算器定義名為Number 的類,其中有兩個(gè)整型數(shù)據(jù)成員n1和n2應(yīng)聲明為私有編寫構(gòu)造方法賦予 n1 和 n2 的初始值再為該類定義 加、減、乘、除等公有實(shí)例方法分別對(duì)兩個(gè)成員變量執(zhí)行加減乘除的運(yùn)算*/ public class Test01{public static void main(String[] args) {Number number = new Number(1,0);number.addition();number.subtration();number.multiplication();number.division();} }class Number{private int n1;private int n2;public Number(){}public Number(int n1,int n2){this.n1=n1;this.n2=n2;}public int getN1() {return n1;}public void setN1(int n1) {this.n1 = n1;}public int getN2() {return n2;}public void setN2(int n2) {this.n2 = n2;}public void addition(){System.out.println(this.getN1()+"+"+this.getN2()+"="+(this.getN1()+this.getN2()));}public void subtration(){System.out.println(this.getN1()+"-"+this.getN2()+"="+(this.getN1()-this.getN2()));}public void multiplication(){System.out.println(this.getN1()+"*"+this.getN2()+"="+(this.getN1()*this.getN2()));}public void division(){if(this.getN2()==0){System.out.println("除數(shù)不能為0");return;}System.out.println(this.getN1()+"/"+this.getN2()+"="+(this.getN1()/this.getN2()));} }測(cè)試結(jié)果:
總結(jié)
以上是生活随笔為你收集整理的JAVA入门级教学之(简单的程序测试)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 编写五子棋的完整python代码_pyt
- 下一篇: 金坛区实验幼儿园服务器不稳定,2019年