zoukankan      html  css  js  c++  java
  • 类和对象的基础题

    1.按要求编写Java应用程序:

    1)编写西游记人物类(XiYouJiRenWu

    其中属性有:身高(height),名字(name),武器(weapon

    方法有:显示名字(printName),显示武器(printWeapon)

    2)在主类的main方法中创建二个对象:zhuBaJiesunWuKong。并分别为他们的两个属性(name,weapon)赋值,最后分别调用printName, printWeapon方法显示二个对象的属性值。

    public class XiYouJiRenWu {
                    double height;
                    String name;
                    String Weapon;
                    
                    String printName()
                    {
                        return name;
                    }
                    String printWeapon()
                    {
                        return Weapon;
                    }
    }
    public class TestXiYouJi {
    
        public static void main(String[] args) {
            // TODO 自动生成的方法存根
            XiYouJiRenWu  xyj1=new XiYouJiRenWu();
            xyj1.name ="zhubajie";
            xyj1.Weapon="九齿钉耙";
            XiYouJiRenWu  xyj2=new XiYouJiRenWu();
            xyj2.name="sunwukong";
            xyj2.Weapon="如意金箍棒";
            System.out.println(xyj1.printName()+" "+xyj1.printWeapon());
            System.out.println(xyj2.printName()+" "+xyj2.printWeapon());
        }
    
    }

     2.编写Java应用程序。首先定义一个描述银行账户的Account类,包括成员变量“账号”和“存款余额”,成员方法有“存款”、“取款”和“余额查询”。其次,编写一个主类,  在主类中测试Account类的功能。

    public class Account {
        String account;
    
        double balance; 
    
        //构造方法
        Account(String id)
        {
            account = id;
        }
        //同时存钱
        Account(String id,double ck)
        {
            account = id;
            balance += ck;
        }
    //成员方法
        
        //存钱
        //有参数无返回值
        void cunQian(double ck)
        {
            balance +=ck;
            
            System.out.println("存入"+ck);
        }
        //取钱
        //有参数有返回值
        boolean quQian(double qk)
        {
            if(qk <=balance)
            {
                balance -= qk;
                System.out.println("取出"+qk);
                return true;
            }
            else
            {
                System.out.println("余额不足");
                return false;
            }
        }
        //获取余额
        double getBalance()
        {
            return balance;
        }
        //显示账号和余额
        //无参数无返回值
        void showAccount()
        {
            System.out.println(" 账号= "+" "+account+" 余额 ="+balance);
        }
    
    
    }
    package com.hanqi.test;
    
    public class TestAccount {
            public static void main(String[]args)
            {
                //测试Account
                Account account = new Account("3445",1000);
                account.showAccount();
                account.cunQian(100);
                account.quQian(30);
                account.showAccount();
                if(account.quQian(1080))
                {
                    System.out.println("余额不足");
                }
                account.showAccount();
                
            

    3.编写Java应用程序。首先,定义一个时钟类——Clock,它包括三个int型成员变量分别表示时、分、秒,一个构造方法用于对三个成员变量(时、分、秒进行初始化,还有一个成员方法show()用于显示时钟对象的时间。其次,再定义一个主类——TestClass,在主类的main方法中创建多个时钟类的对象,使用这些对象调用方法show()来显示时钟的时间。

    public class Clock {
            int hour;
            int minute;
            int second;
            
            Clock(int h,int m,int s)
            {
                 hour =h;
                minute =m;
                second =s;
            }
            void show()
            {
                System.out.println("时间为:"+hour+":"+minute+" :"+second+":");
            }
            
    }
    public class TextClock {
    
        public static void main(String[] args) {
            // TODO 自动生成的方法存根
        Clock clock = new Clock(10,23,21);
        clock.show();
        
        }
    
    }

  • 相关阅读:
    PayPal(贝宝)支付接口、文档、IPN
    C# LiveUpdate.exe实现文件在线更新升级
    C# 邮件发送
    VisualStudio11预览
    zen coding for visual studio 2010(vs2010)
    淘宝API开发系列淘宝API相关了解
    多年积累
    专业淘友必不可少的资料,教你如何玩转淘宝!
    极速理解设计模式系列
    ASP.NET开发人员经常使用的三十三种代码
  • 原文地址:https://www.cnblogs.com/miracle-0807/p/5884193.html
Copyright © 2011-2022 走看看