導航:首頁 > 編程語言 > java繼承的編程題

java繼承的編程題

發布時間:2023-08-05 21:13:32

java編程題目,求求大佬救救我

這個題考察的是面向對象三大特性之一的繼承。

子類繼承父類。

項目結構如何所示:

我是馮修遠,如果我的答案對您有幫助的話,請採納以幫助更多的人,如果還有其它的問題,也請關注我,私信我,謝謝!

② java編程:(二)類的繼承

public class DogTest {

public static void main(String[] args) {
dog = new Dog("Mimi ", "3");
dog.setNickname("Baby");
System.out.println(dog.name + "is" + dog.age + " years old");
System.out.println("It』s nickname is " + dog.getNickname());

Animal animal = new Animal("Tom ", "3");
System.out.println(animal.name + animal.run());
}

}

/**
*
*/
package test;

/**
* @author lilin
*
*/
public class Dog extends Animal {

public Dog(String name, String age) {
super(name, age);
}

private String nickname;

public String getNickname() {
return nickname;
}

public void setNickname(String nickname) {
this.nickname = nickname;
}

public String run() {
System.out.println("run by four feet");
return null;
}

}

/**
*
*/
package test;

/**
* @author lilin
*
*/
public class Animal {

protected String name;

protected String age;

public Animal(String name, String age) {
this.name = name;
this.age = age;
}

public String run() {
return "can run";
}

}

③ 一道Java編程題 通過繼承java.Util.Random類設計一個類RandomEX,並實現

Java程序

importjava.util.Random;

publicclassHardWork{
publicstaticvoidmain(String[]args){
RandomEXrand=newRandomEX();
intn=4;
intgroup=10;

for(inti=0;i<group;i++){
.out.printf("第%2d組: ",i+1);
System.out.printf("1~%d長度二進制字元串: %s ",n,rand.nextBinaryString1(n));
System.out.printf("固定%d長度二進制字元串: %s ",n,rand.nextBinaryString2(n));
System.out.printf("1~%d長度十六進制字元串: %s ",n,rand.nextHexString1(n));
System.out.printf("固定%d長度十六進制字元串: %s ",n,rand.nextHexString2(n));
System.out.println();
}
}
}
/**
*隨機數類
*@author馮向科
*@version2016.05.07
*/
classRandomEXextendsRandom{
=1L;

/**
*產生隨機長度為1~n的二進字元串
*@paramn字元串最大長度
*@return長度為1~n的二進制字元串
*/
publicStringnextBinaryString1(intn){
longnum;
StringBuilderstr=newStringBuilder();
intlen;

do{
num=this.nextLong();
str.append(Long.toBinaryString(num));
len=str.length();
}while(len<n);

intsize=this.nextInt(n)+1;

returnstr.substring(0,size);
}

/**
*產生固定長度為n的二進字元串
*@paramn字元串固定長度
*@return固定長度為n的二進字元串
*/
publicStringnextBinaryString2(intn){
longnum;
StringBuilderstr=newStringBuilder();
intlen;

do{
num=this.nextLong();
str.append(Long.toBinaryString(num));
len=str.length();
}while(len<n);

returnstr.substring(0,n).toUpperCase();
}

/**
*產生隨機長度為1~n的十六進字元串
*@paramn字元串最大長度
*@return長度為1~n的十六進制字元串
*/
publicStringnextHexString1(intn){
longnum;
StringBuilderstr=newStringBuilder();
intlen;

do{
num=this.nextLong();
str.append(Long.toHexString(num));
len=str.length();
}while(len<n);

intsize=this.nextInt(n)+1;

returnstr.substring(0,size).toUpperCase();
}

/**
*產生固定長度為n的十六進字元串
*@paramn字元串固定長度
*@return固定長度為n的十六進字元串
*/
publicStringnextHexString2(intn){
longnum;
StringBuilderstr=newStringBuilder();
intlen;

do{
num=this.nextLong();
str.append(Long.toHexString(num));
len=str.length();
}while(len<n);

returnstr.substring(0,n).toUpperCase();
}
}


運行測試:

第 1 組:

1~4長度二進制字元串: 11

固定4長度二進制字元串: 1010

1~4長度十六進制字元串: 44B

固定4長度十六進制字元串: A7C3


第 2 組:

1~4長度二進制字元串: 110

固定4長度二進制字元串: 1110

1~4長度十六進制字元串: E96D

固定4長度十六進制字元串: 61F1


第 3 組:

1~4長度二進制字元串: 101

固定4長度二進制字元串: 1111

1~4長度十六進制字元串: B1C

固定4長度十六進制字元串: A0F1


第 4 組:

1~4長度二進制字元串: 1101

固定4長度二進制字元串: 1010

1~4長度十六進制字元串: 1DC

固定4長度十六進制字元串: D38E


第 5 組:

1~4長度二進制字元串: 1

固定4長度二進制字元串: 1110

1~4長度十六進制字元串: 70D8

固定4長度十六進制字元串: 495B


第 6 組:

1~4長度二進制字元串: 1100

固定4長度二進制字元串: 1011

1~4長度十六進制字元串: F6

固定4長度十六進制字元串: 5086


第 7 組:

1~4長度二進制字元串: 100

固定4長度二進制字元串: 1100

1~4長度十六進制字元串: 86A

固定4長度十六進制字元串: 23A0


第 8 組:

1~4長度二進制字元串: 1

固定4長度二進制字元串: 1101

1~4長度十六進制字元串: 8

固定4長度十六進制字元串: F967


第 9 組:

1~4長度二進制字元串: 110

固定4長度二進制字元串: 1000

1~4長度十六進制字元串: F194

固定4長度十六進制字元串: 7C3D


第 10 組:

1~4長度二進制字元串: 1101

固定4長度二進制字元串: 1100

1~4長度十六進制字元串: 4

固定4長度十六進制字元串: FB2A

閱讀全文

與java繼承的編程題相關的資料

熱點內容
若有以下程序void 瀏覽:432
大數據主體有哪些 瀏覽:961
如何學習編程的優點 瀏覽:906
最新版本手機qq 瀏覽:463
簡述在word 瀏覽:528
qq怎麼清楚歷史記錄防止被盜 瀏覽:263
發送手機里的錄音文件在哪裡 瀏覽:866
js獲取下一個兄弟元素 瀏覽:293
js模板引擎原理 瀏覽:72
linuxo文件運行 瀏覽:713
什麼免費備份數據 瀏覽:342
測量大師導入底圖找不到文件 瀏覽:313
小紅傘安裝程序要求版本6 瀏覽:799
全民k歌pcm文件夾 瀏覽:224
c圖片寫入資料庫 瀏覽:466
c串口程序 瀏覽:500
中農金融注冊app有哪些 瀏覽:629
回收站文件清理不在電腦上有痕跡 瀏覽:886
dx版本更新 瀏覽:738
主機集群教程 瀏覽:939

友情鏈接