接⼝的java编程题_Java接⼝编程题练习
题⽬1:创建Person接⼝(即“⼈”),它有setData()和getData()⽅法对“⼈”属性name、sex和birthday赋值和获得这些属性组成的字符串信息。创建类Student实现Person接⼝,并对⾃⼰的“学⽣”属性的成员变量sID、speciality设置值和获得它们值所组成的字符串信息。
代码:
People.java(接⼝)
package com.interfaces;
public interface People {
public void setData(String name,String sex,String birthday);
public String getData();
}
Student.java(实现接⼝的类)
java经典上机编程题package com.interfaces.impl;
import com.interfaces.People;
public class Student implements People {
private String name;
private String sex;
private String birthday;
private String sID="2014";
private String speciality="写代码";
@Override
public void setData(String name, String sex, String birthday) {
// TODO Auto-generated method stub
this.name=name;
this.sex=sex;
this.birthday=birthday;
}
@Override
public String getData() {
// TODO Auto-generated method stub
return "名字: " + name + ",性別: " + sex + ",⽣⽇: " + birthday + ",ID: " + sID + ",专长: " +speciality;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
People Yantai=new Student();
Yantai.setData("海哥", "男", "2017年2⽉19⽇");
System.out.Data());
}
}
题⽬2:
编写程序,求柱体的体积:
(1)、为柱体的底⾯设计⼀个接⼝Geometry,包含计算⾯积的⽅法getArea();
(2)、为柱体设计类pillar,要求:
a)有两个成员变量,底⾯和⾼度。底⾯是任何可以计算⾯积的⼏何形状。
b)实现构造⽅法,对成员变量赋值。
c)包含成员⽅法,计算柱体pillar的体积。
(3)、编写测试类圆形类、矩形类实现Geometry接⼝,编写测试类Test,分别⽤圆形、矩形作为柱体的底⾯,并计算其体积。代码:
Geometry.java(接⼝)
package com.interfaces;
public interface Geometry {
public double getArea();shell中for循环语句从1到n
}
Test.java(接⼝实现类)
package com.interfaces.impl;
import com.interfaces.Geometry;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Pillar pillar;
Geometry bottom;
bottom = new Rect(10, 5); //接⼝实现⽅法
pillar = new Pillar(bottom, 5);
System.out.println("矩形底的柱体的体积:" + pillar.Volume());
bottom = new Circle(5);
pillar = new Pillar(bottom, 5);
type的属性值有哪些System.out.println("圆形底的柱体的体积:" + pillar.Volume());
}
/
*
* 柱体设计类
*/
class Pillar{
Geometry bottom;
double height;
public Pillar(Geometry bottom, double height){ this.bottom=bottom;
this.height=height;
}
public double Volume(){
Area()*this.height;
}
}
soapglory>学编程先学什么比较好/*
* 矩形测试类
*/
class Circle implements Geometry{
double radius;
public Circle(double radius){
this.radius = radius;
}
public double getArea() {
/
/ TODO Auto-generated method stub
return Math.PI*this.radius*this.radius;
}
}
/*
* 圆形测试类
*/
class Rect implements Geometry{
double wide,length;
public Rect(double wide, double length){
this.wide = wide;
this.length = length;
}
public double getArea() {
用数字的数组词
return wide*length;
}
}
}  总结:⽹上搜的⾯试题,⾃⼰试着做了做。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。