導航:首頁 > 編程語言 > java介面的陷阱題

java介面的陷阱題

發布時間:2023-03-11 12:39:27

java關於介面的編程題!

關鍵了解介面的作用,你把下面的Cp改成你要的TestSort就OK樂,Cp是我的類文件名 懶得改了interface Sortble{
public int Compare(Sortble s);
}
class Student implements Sortble{
private int score;
Student(int s){
score=s;
}
public int Compare(Sortble s) {
// TODO Auto-generated method stub
Student ss=null;
if(s instanceof Student){
ss=(Student)s;
}else{
System.out.println("程序出錯,意外退出");
System.exit(0);
}
if(this.getScore()>ss.getScore()){
return 1;
}else if(this.getScore()<ss.getScore()){
return -1;
}else{
return 0;
}
}
public String toString(){
return ""+getScore();
}
public void setScore(int score) {
this.score = score;
}
public int getScore() {
return score;
}
}
class Rectangle implements Sortble{
private int length,width;
Rectangle(int length,int width){
this.length=length;
this.width=width;
}
public int area(){
return length*width;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int Compare(Sortble s) {
Rectangle ss=null;
// TODO Auto-generated method stub
if(s instanceof Rectangle){
ss=(Rectangle)s;
}else{
System.out.println("程序出錯,意外退出");
System.exit(0);
}
if(this.area()>ss.area()){
return 1;
}else if(this.area()<ss.area()){
return -1;
}else{
return 0;
}
}
public String toString(){
return ""+area();
}
}
class Sort{
public static void SelectSort(Sortble[] a){
Sortble m=null;
for(int i=0;i<a.length-1;i++){//升序
for(int j=i+1;j<a.length;j++){
if(a[j].Compare(a[i])<0){
m=a[i];
a[i]=a[j];
a[j]=m;
}
}
}
}
}
public class Cp{
Cp(){
Student[] s=new Student[5];
for(int i=0;i<s.length;i++){
s[i]=new Student((int)(Math.random()*100));
}
Sort.SelectSort(s);
System.out.println("下面是按升序輸出學生成績");
for(int i=0;i<s.length;i++){
System.out.println(s[i]);
}
Rectangle[] ss=new Rectangle[5];
for(int i=0;i<ss.length;i++){
ss[i]=new Rectangle((int)(Math.random()*100),(int)(Math.random()*100));
}
Sort.SelectSort(ss);
System.out.println("下面是按升序輸出矩形面積");
for(int i=0;i<ss.length;i++){
System.out.println(ss[i]);
}
}
public static void main(String[] arg){
new Cp();
}
}

❷ 有關介面回調的java題,求解。

下面是UnitPrice介面代碼

public interface UnitPrice {

public double unitprice();

}

下面是電視機類代碼:

public class Television implements UnitPrice {

@Override

public double unitprice() {

return 3500d;

}

}

下面是計算機類代碼:

public class Computer implements UnitPrice {

@Override

public double unitprice() {

return 7800d;

}

}

下面是手機類代碼:

public class Mobile implements UnitPrice {

@Override

public double unitprice() {

return 2500d;

}

}

下面是公共類代碼:

import java.util.Scanner;


public class Common {

static double get(UnitPrice u) {

return u.unitprice();

}

@SuppressWarnings("resource")

public static void main(String[] args) {

System.out.print("請輸入電視機的銷售個數:");

Scanner scanner = new Scanner(System.in);

int television_num = scanner.nextInt();

System.out.print("請輸入計算機的銷售個數:");

scanner = new Scanner(System.in);

int computer_num = scanner.nextInt();

System.out.print("請輸入手機的銷售個數:");

scanner = new Scanner(System.in);

int mobile_num = scanner.nextInt();

double television_price = television_num * (get(new Television()));

System.out.println("電視機的銷額是:" + television_price);

double computer_price = computer_num * (get(new Computer()));

System.out.println("計算機的銷額是:" + computer_price);

double mobile_price = mobile_num * (get(new Mobile()));

System.out.println("手機的銷額是:" + mobile_price);

System.out.println("總銷額是:" + (television_price + computer_price + mobile_price));

scanner.close();

}

}

下面是執行後的效果截圖:

麻煩您看一下,是否能夠滿足要求。

❸ java介面問題

樓主你好!

按您的要求已經完成了初步設計,就等你接收了。

我所用的技術的「java設計模式當中的策略模式」,它的核心點就是封裝演算法。你可以通過自己的編寫新的交通工具在不修改原代碼的情況下按要求對新類進行編碼。

注意:一.你要編寫的交通工具類必須實現Vehicel這個abstract

如:publicclassShipextendsVehicel、、、、

二.因為不同的交通工具演算法不同,所以這步我採用的介面對演算法進行抽象定義,並在實際行為類實現的時候實現此借口單獨編寫自己的演算法。

如:

三.記住我的包名:com.qs.strategy。

四.在最後我用了個簡單工程模式用來實列化style參數,把你要填加的類,添加到if判斷裡面就OK了。

代碼欣賞:

packagecom.qs.strategy;

publicabstractclassVehicle{

;

publicVehicle(){}

();

publicvoidcomputeTime(){

computeTime.computeTime();

}

(){

returncomputeTime;

}

publicvoidsetComputeTime(IComputeTimecomputeTime){

this.computeTime=computeTime;

}

}

packagecom.qs.strategy;

publicclassCarextendsVehicle{

@Override

publicvoidprepareCompute(){

//TODOAuto-generatedmethodstub

System.out.println("交通工具運行1000公里所需的時間:");

System.out.println("==========================");

}

}

packagecom.qs.strategy;

{

@Override

publicvoidprepareCompute(){

//TODOAuto-generatedmethodstub

System.out.println("交通工具運行1000公里所需的時間:");

System.out.println("==========================");

}

}

packagecom.qs.strategy;

publicinterfaceIComputeTime{

voidcomputeTime();

}

packagecom.qs.strategy;

{

privateinta;

privateintb;

privateintc;

publicPlaneWithComputeTime(){}

publicPlaneWithComputeTime(inta,intb,intc){

this.a=a;

this.b=b;

this.c=c;

}

publicvoidcomputeTime(){

//TODOAuto-generatedmethodstub

System.out.println("javaComputeTimePlane"+""+a+""+b+""+c);

System.out.println("computeresultis"+(a+b)+c);

}

}

packagecom.qs.strategy;

{

privateinta;

privateintb;

privateintc;

publicCarWithComputeTime(){}

publicCarWithComputeTime(inta,intb,intc){

this.a=a;

this.b=b;

this.c=c;

}

publicvoidcomputeTime(){

//TODOAuto-generatedmethodstub

System.out.println("javaComputeTimeCar"+""+a+""+b+""+c);

System.out.println("computeresultis"+(a*b)/c);

}

}

packagecom.qs.strategy;

{

publicSimpleVehicleFactory(){

}

publicVehicleinstance(Stringstyle){

Vehiclevehicle=null;

if(style.toLowerCase().equals("car")){

vehicle=newCar();

}elseif(style.toLowerCase().equals("plane")){

vehicle=newPlane();

}

if(vehicle==null)

System.out.println("請與管理員聯系你輸入的交通工具還未開通!!!");

returnvehicle;

}

}

最後是測試類:

packagecom.qs.strategy;

publicclassTestVehicle{

/**

*@paramargs

*/

publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstub

SimpleVehicleFactoryfactory=newSimpleVehicleFactory();

if(factory.instance("CaRs")!=null){

Vehiclevehicle=factory.instance("CaRs");

vehicle.prepareCompute();

vehicle.setComputeTime(newCarWithComputeTime(40,50,60));

vehicle.computeTime();

}

}

}

閱讀全文

與java介面的陷阱題相關的資料

熱點內容
發文件給同事怎麼說 瀏覽:468
蘋果80歲用什麼app 瀏覽:28
順豐寄快遞文件多少錢 瀏覽:164
消費邦app是怎麼反現的 瀏覽:112
java調用介面方法 瀏覽:742
微信一種以上綁定關系 瀏覽:183
word圖片編輯大小邊框 瀏覽:468
威迅java培訓 瀏覽:389
linux禪道無法訪問 瀏覽:819
怎麼爬取歷史疫情數據 瀏覽:596
linuxjira6破解 瀏覽:694
哪個網站可以看所有檢察雜志 瀏覽:144
java高並發資料庫請求怎麼辦 瀏覽:551
win8怎麼打開gho文件怎麼打開 瀏覽:732
如何網站內搜索 瀏覽:362
qq附近的人客服號碼 瀏覽:570
mac怎麼把word文件轉換為pdf 瀏覽:6
正式文件中的文檔行距多少 瀏覽:202
vu用upload上傳excel文件 瀏覽:544
win10m裝安卓軟體 瀏覽:83

友情鏈接