看程序写结果

本文通过一系列示例代码探讨了Java编程中的类构造过程,包括静态代码块、构造代码块、构造方法以及this和super关键字的使用。详细解释了对象创建时的执行顺序,如静态初始化块、普通初始化块、构造方法的调用,并讨论了继承、内部类以及接口的实现和继承等核心概念。
摘要由CSDN通过智能技术生成

1.(静态代码块)

class Student{
    static{System.out.println("Student的静态代码块1") ;}
    public Student(){
        System.out.println("Student的无参构造方法") ;
    }
    {System.out.println("Student的构造代码块1") ;}
    {System.out.println("Student的构造代码块2") ;}
    public Student(String str){
        System.out.println("Student的有参构造方法") ;
    }
    static{System.out.println("Student的静态代码块2") ;} 
}
class StudentTest{
    static{System.out.println("高圆圆已经42了,我很伤心") ;}
    public static void main(String[] args){
          Student s = new Student() ;
          Student s2 = new Student("hello") ;
    }
}


高圆圆已经42了,我很伤心
Student的静态代码块1
Student的静态代码块2
Student的构造代码块1
Student的构造代码块2
Student的无参构造方法
Student的构造代码块1
Student的构造代码块2
Student的有参构造方法

2.(this  super)

需要在控制台分别打印30,20,10

        class Test{
        public static void main(String[] args){

            Zi zi = new Zi();
            zi.show() ;    
        }
        }

        class Fu{
            int num = 10;
        }
        class Zi extends Fu{
            int num = 20 ;
            public void show(){
            int num = 30 ;
            //补全代码


        System.out.println(num);//30
        System.out.println(this.num);//20
        System.out.println(super.num);//10
        }
        }

 3.(继承、对象构造过程)

有以下代码
class Meal{
public Meal(){
    System.out.println("Meal()");

}
class Lunch extends Meal{
public Lunch(){
    System.out.println("Lunch()");
}    
}
class Vegetable {
public Vegetable(){
    System.out.println("Vegetable()");

}
class Potato extends Vegetable{
public Potato(){
    System.out.println("Potato()");

}
class Tomato extends Vegetable{
public Tomato(){
    System.out.println("Tomato()");

}
class Meat{
public Meat(){
    System.out.println("Meat()");

}
class Sandwich extends Lunch{
    Potato p = new Potato();
    Meat m = new Meat();
    Tomato t = new Tomato();
    public Sandwich(){
    System.out.println("Sandwich()");
    } 
}
public class TestSandwich{
    public static void main(String args[]){
    Sandwich s = new Sandwich();
    } 
}
写出这段代码的输出结果。


Meal()
Lunch()
Vegetable()
Potato()
Meat()
Vegetable()
Tomato()
Sandwich()

 4.

看程序,写结果:(请说明原因)
  class Root {
  static {
    System.out.println("Root的静态初始化块");
  }
 
  {
    System.out.println("Root的普通初始化块

java程序设计》课程的题库资料,由贺州学院整理,可供学生期末课程复习使用,也可以供相关任课教师卷使用。 内容示例为: 9. 阅读下列程序片段,运行结果。 public class Test { public static void main(String[] args) { int percent = 10; tripleValue(percent); System.out.println(percent); } public static void tripleValue(int x) { x = 3 * x; } } 代码执行后输的结果是______。 答案:10 [解析] static 关键字应用的场合有:①用来修饰类中定义的变量,这样的变量称为类变量或静态变量。②可以用来修饰类中定义的方法,这样的方法称为静态方法。③用来修饰初始化语句块,这样的语句块常称为静态初始化语句块。static 在这里表示这个方法为类方法,不属于任何对象实例,而是类所有,描述对象的共有动作,可以用类名直接调用。在调用了tripleValue函数之后,函数的值没有返回,所以percent的值还是10。 10. 阅读下列程序片段,运行结果。 class Shape { public Shape() { System.out.print("Shape"); } } class Circle extends Shape { public Circle() { System.out.print("Circle"); } } public class Test { public static void main(String[] args) { Shape d = new Circle(); } } 代码执行后输的结果是______。 答案:ShapeCircle [解析] 继承是而向对象编程的一个主要优点之一,它对如何设计Java类有着直接的影响。继承有如下几点好处: ①它可以利用已有的类来创建自己的类,只需要指自己的类和已有的其他类有什么不同即可,而且还可以动态访问其他有 关类中的信息。 ②通过继承,可以利用Java类库所提供的丰富而有用的类,这些类都已经被很好地实现。 ③当设计很大的程序时,继承可以使程序组织得层次清晰,有利于程序设计相减少错误的发生。该程序首先编了一个Shape的类,然后又编一个类Circle去继承Shape类。由于子类拥有父类所有的属性和方法,所以输的是ShappeCircle。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值