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