Java 接口编程题练习
来源:互联网 发布:2016淘宝小号注册方法 编辑:程序博客网 时间:2024/05/21 10:10
题目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(实现接口的类)
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="写代码"; @Overridepublic void setData(String name, String sex, String birthday) {// TODO Auto-generated method stub this.name=name; this.sex=sex; this.birthday=birthday;}@Overridepublic 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.println(Yantai.getData()); }}
题目2:
编写程序,求柱体的体积:
(1)、为柱体的底面设计一个接口Geometry,包含计算面积的方法getArea();
(2)、为柱体设计类pillar,要求:
a)有两个成员变量,底面和高度。底面是任何可以计算面积的几何形状。
b)实现构造方法,对成员变量赋值。
c)包含成员方法,计算柱体pillar的体积。
(3)、编写测试类圆形类、矩形类实现Geometry接口,编写测试类Test,分别用圆形、矩形作为柱体的底面,并计算其体积。
代码:
Geometry.java(接口)
package com.interfaces;public interface Geometry { public double getArea();}
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); 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(){ return bottom.getArea()*this.height; } }/* * 矩形测试类 */class Circle implements Geometry{ double radius;public Circle(double radius){ this.radius = radius; } public double getArea() {// TODO Auto-generated method stubreturn 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; } } }
总结:网上搜的面试题,自己试着做了做。
0 0
- Java 接口编程题练习
- java编程题练习
- java接口编程题
- Java编程题练习(一)
- Java编程题练习(二)
- Java接口练习
- java接口小练习
- Java编程题练习2017-02-06
- Java编程题练习2017-02-07
- Java编程题练习2017-02-08
- Java编程题练习2017-02-12
- Java编程题练习2017-02-13
- Java编程题练习2017-02-14
- Java编程题练习2017-02-15
- Java编程题练习2017-02-16
- Java编程题练习2017-02-17
- Java编程题练习2017-02-18
- Java编程题练习2017-02-19
- 关于mysql的安装使用以及java编程
- maven笔记5--版本管理
- 欢迎使用CSDN-markdown编辑器
- iOS 创建OpenGL 环境的思考
- leetcode 129. Sum Root to Leaf Numbers
- Java 接口编程题练习
- js返回顶部及重载
- nginx单独模块编译的makefile编写
- 如何提升自信心
- 温习static和const在C和C++的区别
- Java中BufferedReader和scanner的对比
- KMP算法
- MySQL 时间差、每15分钟统计一次数据
- Python基础差缺补漏