Java作业三 语言基础与面向对象


掌握Java语言基础与面向对象的综合编程技术与方法,更全面深入地理解面向对象程序设计技术内涵。

一、阅读程序题1

请先阅读下面的程序,确定它的输出结果。再将程序上机运行,验证自己分析的输出结果是否正确。

package Practice3;

class Yuan
{
   
    int r;
    String name;
    static int z= 5;
    Yuan(int r){
    this.r=r; }
    int a(){
    return z*r*r; }
    int p(){
    return 5*z*r; }
    void value (){
    name="china"; }
}
class testOverride extends Yuan
{
   
    int h;
    String name;
    testOverride(int r,int h1) {
    super(r); h=h1; }
    int a(){
    value();  return 2*super.a()+p()*h; }

    void value()
    {
   
        super.value();
        name="America";
        System.out.println("\""+this.name+"\"");
        System.out.println(super.name);
    }

    public static void main(String args[])
    {
   
        Yuan y= new Yuan(4);
        Yuan yz= new testOverride(5,2);
        System.out.println(y.a());
        System.out.println(yz.p());
        System.out.println(yz.a());
    }
}

自己运行一下就好了,结果如下:

80
125
"America"
china
500

二、阅读程序题2

请先阅读下面的程序,确定它的输出结果。再将程序上机运行,验证自己分析的输出结果是否正确。

public class TestTransOfValue
{
   
    public static void main(String args[]) 
    {
    
        double val;
        StringBuffer sb1, sb2;
        String sb3;
        char s[]={
   'a','p','p','l','e'};
        val = 5.8;
        sb1 = new StringBuffer("apples");
        sb2=new StringBuffer("pears");
        sb3 = new String("pear");
        modify(val, sb1, sb2,sb3,s);
        System.out.println(val);
        System.out.println(sb1);
        System.out.println(sb2);
        System.out.println(sb3);
        System.out.println(s); 
    }
    public static void modify(double a, StringBuffer r1, StringBuffer r2,String r3,char s[] ) 
    {
    
        a = 6.8;
        r1.append(" taste good");
        r2=null;
        r3="banana";
        s[2]='R';       
    }
}

结果如下:

5.8
apples taste good
pears
pear
apRle

三、阅读程序题3

请仔细阅读下面的程序,分析程序的结构和输出结果。再将程序上机运行,验证输出结果。(注意重点理解:内部类、对象内部类、静态内部类)

package Practice3;

public class Outer
{
   
    public Outer()
    {
   
        System.out.println("OuterClass Object!");
    }

    private class Inner1
    {
   
        private
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值