1.说出下面程序的执行结果
interface InterfaceA {
String S = "good ";
void f();
}
abstract class ClassA {
abstract void g();
}
class ClassB extends ClassA implements InterfaceA {
void g() {
System.out.print(S);
}
public void f() {
System.out.print(" "+ S);
}
}
public class Test {
public static void main(String[] args) {
ClassA a = new ClassB();
InterfaceA b = new ClassB();
a.g();
b.f();
}
}
2.利用接口做参数,写个计算器,能完成加减乘除运算。
/**
定义一个接口Compute含有一个方法int computer(int n, int m)。
*/
interface ICompute{
int computer(int n, int m);
}
/**
* 加法类
* @author Z7M-SL7D2
*
*/
class AddImpl implements ICompute{
public int computer(int n, int m){
return n + m;
}
}
/**
* 减法类
* @author Z7M-SL7D2
*
*/
class SubImpl implements ICompute{
public int computer(int n, int m){
return n - m;
}
}
/**
* 乘法类
* @author Z7M-SL7D2
*
*/
class MulImpl implements ICompute{
public int computer(int n, int m){
return n * m;
}
}
/**
* 除法类
* @author Z7M-SL7D2
*
*/
class DivImpl implements ICompute{
public int computer(int n, int m){
return n / m;
}
}
/**
UseCompute 类,用来对计算机进行操作
*/
class UseCompute {
public void useCom(ICompute com, int one, int two){
int ret = com.computer(one, two);
System.out.println("计算结果为:" + ret);
}
}
/**
* 主类Test,调用UseCompute中的方法useCom来完成加减乘除运算。
* @author Z7M-SL7D2
*
*/
public class Test {
public static void main(String[] args) {
UseCompute uc = new UseCompute();
uc.useCom(new AddImpl(), 5, 6);
}
}
3. 接口练习
/**
* 接口A,里面包含值为3.14的常量PI和抽象方法double area()。
* @author Z7M-SL7D2
*
*/
interface A{
float PI = 3.14f;
double area();
}
/**
* 接口B,里面包含抽象方法void setColor(String c)
* @author Z7M-SL7D2
*
*/
interface B{
void setColor(String c);
}
/**
* 接口C,该接口继承了接口A和B,里面包含抽象方法void volume()
* @author Z7M-SL7D2
*
*/
interface C extends A, B{
void volume();
}
/**
* 圆柱体类Cylinder实现接口C,该类中包含三个成员变量:底圆半径radius、
圆柱体的高height、颜色color。
* @author Z7M-SL7D2
*
*/
class Cylinder implements C{
private int radius;
private int height;
private String color;
public Cylinder(){}
public Cylinder(int radius, int height, String color){
this.radius = radius;
this.height = height;
this.color = color;
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public String getColor() {
return color;
}
@Override
public double area() {
return radius*2*PI*height + radius*radius*PI;
}
@Override
public void setColor(String c) {
}
@Override
public void volume() {
float volumeCount = radius*radius*PI;
System.out.println("体积为:" + volumeCount);
}
}
/**
* 主类用来测试类Cylinder。
* @author Z7M-SL7D2
*
*/
public class Test{
public static void main(String[] args) {
Cylinder c = new Cylinder(2, 2, "blue");
System.out.println("表面积为:" + c.area());
c.volume();
}
}
4.完数
//一个数如果恰好等于它的因子之和,这个数就称为 "完数 "。例如6=1+2+3.编程 找出1000以内的所有完数。
public class Test{
public static void fun(){
for(int i = 2; i<1000; i++){
int sum = 1;
//判断是奇数还是偶数,奇数的因子没有偶数
if(i%2 == 0){
for(int j = 2; j <= i/2; j++ ){
if(i%j == 0)
sum+=j;
if(sum > i)
break;
}
}else{
for(int j = 1; j <= i/2; j+=2 ){
if(i%j == 0)
sum+=j;
if(sum > i)
break;
}
}
//输出结果
if(sum == i)
System.out.println(i);
}
}
public static void main(String[] args) {
fun();
}
}
输出结果如下: