java抽象与封装_一些java代码(抽象,封装)

请指出如下Java代码中存在的错误,并解释原因

class Other {

public int i;

}

class Something {

public static void main (String[] args) {

Other o = new Other();

new Something().addOne(o);

}

public void addOne (final Other o) {

o.i++;

o = new Other();//常量不能被重新赋值

}

}

下列代码运行结果

class Person {

String name = "person";

public void shout () {

System.out.print(name);

}

}

class Student extends Person {

String name = "student";//注意这里

String school = "school";

}

class Test {

public static void main (String[] args) {

Person p = new Student();

p.shout();//这里的输出结果是person,因为Person类里面的name和Student类里的name是两个属性。

}

}

如何想要输出Student类的name属性值可以这样写

class Student extends Person {

String name = "student";//注意这里

String school = "school";

public void shout () {

System.out.print(this.name);//调用该类的name属性

System.out.print(super.name);//调用父类的name属性

}

}

指出下列代码中出现的错误,并解释原因

/*

* 几何图形

*/

abstract class Shape {

public abstract double getArea();

}

/*

* 正方形

*/

class Square extends Shape {

private double sideLength = 0;//正方形的边长

//1.当一个类在没有手动编写构造方法的时候,系统会自动提供一个无参构造方法

//2.当一个类中有编写构造方法的时候,系统就不会提供无参构造方法,所以这里没有无参构造方法。

public Square (double sideLength) {

this.sideLength = sideLength;

}

public double getArea () {

return (this.sideLength*this.sideLength);//返回面积

}

}

/*

* 长方体

*/

class Cuboid extends Square {

private double height = 0;//正方体的高

public Cuboid (double height) {

//此处没有调用其他构造方法,系统会自动调用父类的无参构造方法,相当于super();

this.height = height;

}

public double getArea () {

return (super.getArea()*this.height);//返回体积

}

}

class TestShape {

public static void main (String[] args) {

Shape square = new Square(3);

Shape cuboid = new Cuboid(3,2);

System.out.println(square.getArea());

System.out.println(cuboid.getArea());

//向下转型,Shape —> Square,强制类型转换其实是还原对象的真实面目,new Cuboid(3,2)对象的真实类型为Cuboid类

Square sq = (Square) cuboid;

//这里不管cuboid转换成什么类型,它调用的方法始终是Cuboid类里面的方法。

System.out.println(sq.getArea());

}

}

//改法如下

class Cuboid extends Square {

private double height = 0;//正方体的高

public Cuboid (double sideLength, double height) {

super(sideLength);

this.height = height;

}

public double getArea () {

return (super.getArea()*this.height);//返回体积

}

}

class StaticCodeBlock {

public static String name = "defname";

static {

name = "staticname";

System.out.println("execute static code block");

}

public StaticCodeBlock () {

System.out.println("execute constructor");

}

}

public class TestStaticCodeBlock {

static {

System.out.println("execute static code block in Test");

}

public static void main (String[] args) {

System.out.println("execute main()");

new StaticCodeBlock();

new StaticCodeBlock();

new StaticCodeBlock();

}

}

这行代码的运行结果为

execute static code block in Test

execute main()

execute static code block

execute constructor

execute constructor

execute constructor

abstract class Person {

private int num = 3;

public abstract void show();

}

class Child extends Person {

public void show () {

System.out.println(super.num);//编译到这里会报错,因为num是私有的属性,子类并不会继承这一属性,也不能调用

}

public static void main (String[] args) {

Child child = new Child();

child.show();

}

}

如何在子类中调用父类的私有属性:

在Person类中加入一个get方法

public int getNum () {

return this.num;

}

public interface Test1 {

public static final int num = 0;//接口中属性默认用public static final修饰

// 接口和抽象类不同之处,接口不允许有静态代码块,这是因为接口不能new

// static {

//

// }

/*

* 接口中允许有静态方法

*/

public static void staticMethod () {

System.out.println("233");

}

/*

* 接口中除了static修饰的方法以外,其他的方法默认用public abstract修饰

*/

public abstract void show ();

}

/*

* 抽象类可以继承普通类和抽象类,接口只能继承接口

*/

public abstract class Test2 extends Test4 {

public static final int num = 3;//抽象类中可以有全局静态常量

/*

* 抽象类中可以有构造方法

*/

public Test2 () {

System.out.println("调用Test2的构造方法");

}

/*

* 抽象类中可以有静态代码块

*/

static {

//在加载类的过程中,先完成静态变量的内存分配,然后再执行静态块

System.out.println("num = " + num);

System.out.println("Test2类加载完成时执行静态代码块");

}

/*

* 抽象类中可以有静态方法

*/

public static void ass () {

}

}

public class Test3 extends Test2 {

public Test3 () {

System.out.println("调用Test3的构造方法");

}

static {

System.out.println("Test3类加载完成时执行静态代码块");

}

public static void main(String[] args) {

// TODO Auto-generated method stub

//new Test3();

Test1.staticMethod();//接口的静态方法同样可以通过类名.方法名()调用

}

}

public class Test4 {

public static void main(String[] args) {

// TODO Auto-generated method stub

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值