面向对象习题


1.简述重载(Overload)与重写(Override)的区别。

重载 overload

重载的作用是类多态性的体现,多个同名函数同时存在。
重载的定义:函数名相同,参数顺序不同,参数类型不同,参数个数不同其中任意一个不同都是重载方法,


注意:
普通方法可以重载,构造方法也可以重载,
且方法重载是在同一个类中
方法重载与返回值无关,对访问权限无要求


重写 override
在Java中,子类可继承父类中的方法,而不需要重新编写相同的方法。
但有时子类并不想原封不动地继承父类的方法,而是想作一定的修改,这就需要采用方法的重写。
方法重写又称方法覆盖
定义:当子类从父类继承的方法不能满足子类需求时,在子类中对该方法重写,建议在该方法前面加上@Override,
编译器可自动检查函数名,参数列表是否一致,返回值类型是否一致,或者子类方法返回值类型是父类方法返回值类型的子类。


重写的注意事项:
只能存在于子类和父类之间
权限不能比父类方法更加严格
重写的前提是先继承了父类的方法
@Override语句和子类方法之间不能有其他语句;

重写方法只能存在于具有继承关系中,重写方法只能重写父类非私有的方法


2.完成刺客刺杀总统的场景,要求:刺杀成功后,将刺客的名字和总统的名字
  都显示出来。

package li.xiao.feng3;


public class assassin {
private String name;
private String sex;
private int  height;
public assassin()
{
}
public assassin(String name,String sex,int  height)
{
this.height=height;
this.name=name;
this.sex=sex;
}
public void show()
{
System.out.println("姓名为:"+name+"    性别:"+sex+"   身高:  "+height);
}
}


package li.xiao.feng3;


public class PresidentKilled {
 private  String name;
   private  String sex;
    PresidentKilled ()
    {
    }
    PresidentKilled (String name,String sex)
    {
    this.name=name;
    this.sex=sex;
    }
    public void show()
    {
    System.out.println(" 名字为:  "+name+"   性别为:   "+sex);
    }
  }


package li.xiao.feng3;


public class test_Presentassassin {


public static void main(String[] args) {
assassin ass = new assassin("天曼", "男", 182);
PresidentKilled pres = new PresidentKilled("凡白", "女");
System.out.println("刺客信息为:  ");
ass.show();
System.out.println("被刺杀的总统信息为:");
pres.show();


}
}



3.建立一个长方形的类 包括属性,长、宽 


  1) 在构造方法中,给长方形的属性初始化。


  2) 写两个方法,分别求出长方形的周长和面积。


  3) 写一个方法,以长方形的对象为参数,求出两个长方形的周长差。

package li.xiao.feng3;


public class rectangle_class {


private double length;
private  double wide;
public  rectangle_class()
{

}
public rectangle_class(double length, double wide) {
this.length = length;
this.wide = wide;
}
public double perimeter()
{
return(2*(this.wide+this.length));
}
public double area()
{
return wide*length;
}
public double  gap(rectangle_class a)
{
    return Math.abs(a.perimeter()-this.perimeter());
}
}

package li.xiao.feng3;
import java.util.Scanner;
public class test_rectangle {
              public static void main(String[] args) {
Scanner input =new  Scanner(System.in);
double wide, length;
System.out.println("请输入第一个长方形的长和宽:");
length = input.nextDouble();
wide = input.nextDouble();
rectangle_class rect1 = new rectangle_class(length, wide);
System.out.println("该长方形的周长为:" + rect1.perimeter());
System.out.println("该长方形的面积为:" + rect1.area());
System.out.println("请输入第二个长方形的长和宽:");
length = input.nextDouble();
wide = input.nextDouble();
rectangle_class rect2 = new rectangle_class(length, wide);
System.out.println("两个长方形的周长差为:" + rect2.gap(rect1));
input.close();
}


}

4. 创建拥有继承结构的两个类,两个类拥有相同定义的show()方法,但是方法体中
   的内容不同,分别创建一个父类对象和子类对象,分别调用show()方法,观察结果。

package li.xiao.feng3;
/**
 * 定义账户类Account来建模银行账户
 *一个账户有账号,余额,年利率,
 *开户日期以及存款,取款等方法
 * @author Lenovo
 *
 */


public class Account 
{


private int id=0;//账号,默认为0
   private double balance=0;//余额,默认为0
private double annualinterestrate=0;//年利率,默认为0
private  String datecreated;//开户日期
public Account() 
{
super();

}
//构造函数,初始化账号,余额,年利率,开户日期等
public Account(int id, double balance,double annualinterestrate, String datecreated)
{
super();//继承父类构造函数
this.id = id;
this.balance = balance;
this.annualinterestrate=annualinterestrate;
this.datecreated=datecreated;
}
/**
*  getDraw(double balance)该方法用来从账户提取特定数额
* @param balance 提取的钱数
*/
   public   boolean getDraw(double balance)
    {
    if(this.balance>balance)
    {
    this.balance=this.balance-balance;
            return true;
    }
    else//余额不足
    return false;
    }
/**
* dePosit(double balance)该方法用来往账户存款
* @param balance  存储的钱数
*/
    public void dePosit(double balance)
    {
        this.balance=this.balance+balance;
     }
public double getBalance()
{
return balance;
}

public int getId() {               //账号访问器
return id;
}

public double getAnnualinterestrate() {              //年利率访问器
return annualinterestrate;
}

public String getDatecreated() {       //开户日期访问器
return datecreated;
}

public void setBalance(double balance)  //余额修改器
{
this.balance = balance;
}
public void show()    //显示账户信息
{
   System.out.println("****账户信息****");
   System.out.println("该账户账号为:"+id);
System.out.println("目前余额为:"+balance);
System.out.println("年利率为: "+annualinterestrate);
System.out.println("开户日期为:"+datecreated );
}

}


package li.xiao.feng3;
import java.util.Scanner;
public class test_CheckAccount {


public static void main(String[] args) {
          Scanner  input=new  Scanner(System.in); 
       double balance;
          Checking_acccount    acc1=new Checking_acccount(1122,7000,0.045,"2017-12-12");
         acc1.show();
         System.out.println("请输入存款金额:");
         balance=input.nextDouble();
        
         acc1.dePosit(balance);
         System.out.println("存款之后账户信息为:");
         acc1.show();
         System.out.println("请输入取款金额:");
         balance=input.nextDouble();
         if( acc1.getDraw(balance)==true)
         {
            System.out.println("取款成功!");
        System.out.println("取款之后账户信息为:");
          acc1.show();
   }
         else
         {
        System.out.println("您目前的余额为:"+acc1.getBalance()+"透支限定额为:"+acc1.getLimite()+"  您已使用"+acc1.getUsed()+",透支额不足,无法取款!");
         }
         input.close();
}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值