java接口代码题 java接口编程题
java关于接口的编程题
class Main {
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名注册、虚拟主机、营销软件、网站建设、乳山网站维护、网站推广。
public static void main(String[] args) {
IPerson person = new Person();
person.setFood("西红柿炒鸡蛋");
person.eat();
}
}
interface IPerson{
void eat();
void setFood(String food);
}
class Person implements IPerson{
private String food;
@Override
public void eat() {
System.out.println("正在吃" + food);
}
@Override
public void setFood(String food) {
this.food = food;
}
}
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接口题
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题,求解。
下面是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题目,用接口实现
String clazz="XXX"
Area area=Class.forName(clazz).newinstance();
XXX是实现的你接口的类
这就是传说中的父类的引用指向子类的对象,多态
第二句话相当于
Area area=new XXX();
父类的引用只能调用子类实现的方法,而无法调用子类中存在而父类中不存在的方法
父类的引用指向子类的对象时,其调用方法是用子类的方法去实现的
当前名称:java接口代码题 java接口编程题
路径分享:http://scyanting.com/article/dodidsh.html