「java接口题目」java接口例题

博主:adminadmin 2022-11-22 03:13:06 66

今天给各位分享java接口题目的知识,其中也会对java接口例题进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

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;ia.length-1;i++){//升序

for(int j=i+1;ja.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;is.length;i++){

s[i]=new Student((int)(Math.random()*100));

}

Sort.SelectSort(s);

System.out.println("下面是按升序输出学生成绩");

for(int i=0;is.length;i++){

System.out.println(s[i]);

}

Rectangle[] ss=new Rectangle[5];

for(int i=0;iss.length;i++){

ss[i]=new Rectangle((int)(Math.random()*100),(int)(Math.random()*100));

}

Sort.SelectSort(ss);

System.out.println("下面是按升序输出矩形面积");

for(int i=0;iss.length;i++){

System.out.println(ss[i]);

}

}

public static void main(String[] arg){

new Cp();

}

}

「java接口题目」java接口例题

请帮我分析一下这个代码!关于java中接口的两题目

第一题中

Runner接口没有定义

添加一个接口定义

public

interface

Runner

{

public

void

run();

public

void

start();

public

void

stop();

}

第二题中的的main方法定义有误

可能是你打错了吧

public

static

void

main(String

args)

改为

public

static

void

main(String

args[])

运行是没有问题的

主要用到了多态上转型

java接口题

interface Vehicle {

public void Start(String s);

public void stop(String s);

}

class Bike implements Vehicle {

@Override

public void Start(String s) {

System.out.println(s + " bike start");

}

@Override

public void stop(String s) {

System.out.println(s + " bike stop");

}

}

class Bus implements Vehicle {

@Override

public void Start(String s) {

System.out.println(s + " bus start");

}

@Override

public void stop(String s) {

System.out.println(s + " bus stop");

}

}

class interfaceDemo {

public static void main(String[] args) {

Bike bike = new Bike();

bike.Start("My");

bike.stop("My");

Bus bus = new Bus();

bus.Start("My");

bus.stop("My");

}

}

java接口题目的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java接口例题、java接口题目的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-22,除非注明,否则均为首码项目网原创文章,转载请注明出处。