码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA作业三

时间:2018-04-20 00:08:22      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:显示   公司   输出   show   创建   bre   sys   password   with   

(一)学习总结

1.阅读下面程序,分析是否能编译通过?如果不能,说明原因。应该如何修改?程序的运行结果是什么?为什么子类的构造方法在运行之前,必须调用父 类的构造方法?能不能反过来?

class Grandparent {
    public Grandparent() {
        System.out.println("GrandParent Created.");
    }
    public Grandparent(String string) {
        System.out.println("GrandParent Created.String:" + string);
    }
}
class Parent extends Grandparent {
    public Parent() {        
        System.out.println("Parent Created");
        super("Hello.Grandparent.");
    }
}
class Child extends Parent {
    public Child() {
        System.out.println("Child Created");
    }
}
public class Test{
    public static void main(String args[]) {
        Child c = new Child();
    }
}

运行结果:
技术分享图片
技术分享图片
应该将Parent类中super("Hello.Grandparent.");放在该构造方法的第一句,放在输出之前
修改后运行结果
技术分享图片
只能子类调用父类的成员变量和成员方法,实现继承,不能反过来
2.阅读下面程序,分析程序中存在哪些错误,说明原因,应如何改正?正确程序的运行结果是什么?
···
class Animal{
void shout(){
System.out.println("动物叫!");
}
}
class Dog extends Animal{
public void shout(){
System.out.println("汪汪......!");
}
public void sleep() {
System.out.println("狗狗睡觉......");
}
}
public class Test{
public static void main(String args[]) {
Animal animal = new Dog();
animal.shout();
animal.sleep();
Dog dog = animal;
dog.sleep();
Animal animal2 = new Animal();
dog = (Dog)animal2;
dog.shout();
}
}
···
运行结果
技术分享图片
第一行错误的原因是上转型,只能调用子类继承或者覆写的方法,其中没有sleep方法。 将其去掉
第二行错误的原因是下转型需要加上“(类型)“, 改正:Dog dog =(Dog) animal;
运行结果
技术分享图片

3.运行下列程序
···
class Person {
private String name ;
private int age ;
public Person(String name,int age){
this.name = name ;
this.age = age ;
}
}
public class Test{
public static void main(String args[]){
Person per = new Person("张三",20) ;
System.out.println(per);
System.out.println(per.toString()) ;
}
}
···
(1)程序的运行结果如下,说明什么问题?
···
Person@166afb3
Person@166afb3
···
输出类对象会默认调用object类的tostring方法
(2)那么,程序的运行结果到底是什么呢?利用eclipse打开println(per)方法的源码,查看该方法中又调用了哪些方法,能否解释本例的运行结果?
技术分享图片
出来了这个。。
(3)在Person类中增加如下方法
···
public String toString(){
return "姓名:" + this.name + ",年龄:" + this.age ;
}
···
重新运行程序,程序的执行结果是什么?说明什么问题?
可参考教材P229
运行结果
技术分享图片
覆写了toString方法,调用的为覆写后的方法,打印类名也默认调用覆写后的方法
4.汽车租赁公司,出租汽车种类有客车、货车和皮卡三种,每辆汽车除了具有编号、名称、租金三个基本属性之外,客车有载客量,货车有载货量,皮卡则同时具有载客量和载货量。用面向对象编程思想分析上述问题,将其表示成合适的类、抽象类或接口,说明设计思路。现在要创建一个可租车列表,应当如何创建?

创建思路:
将编号、名称、租金定义为接口1,载客量为一个接口2,载货量为一个接口3
客车类连接接口1和2,货车连接接口1和3,皮卡连接接口1,2,3
5.阅读下面程序,分析代码是否能编译通过,如果不能,说明原因,并进行改正。如果能,列出运行结果
···
interface Animal{
void breathe();
void run();
void eat();
}
class Dog implements Animal{
public void breathe(){
System.out.println("I‘m breathing");
}
void eat(){
System.out.println("I‘m eating");
}
}
public class Test{
public static void main(String[] args){
Dog dog = new Dog();
dog.breathe();
dog.eat();
}
}
···
修改后
···
package 作业三;
interface Animal{
void breathe();
void run();
void eat();
}
class Dog implements Animal{
public void breathe(){
System.out.println("I‘m breathing");
}
public void run() {
System.out.println("I‘m running");
}
public void eat(){
System.out.println("I‘m eating");
}
}
public class test{
public static void main(String[] args){
Dog dog = new Dog();
dog.breathe();
dog.eat();
}
}
···
运行结果
技术分享图片

6.其他需要总结的内容。
实验四
银行类
···package test;

public class Bank {
static String bankName;//银行名称
private String name;
private String password;
private double balance;//账户余额
private double turnover;//交易额

static void welcome()
{
    System.out.println("欢迎来到建设银行");
}

//开户
public Bank(String name,String password,double turnover){
    this.name = name;
    this.password= password;
    this.turnover = turnover;
    this.balance = turnover - 10;
    
    System.out.println(name+"开户成功"+"余额为:"+balance);
}

//存款方法
public void depoist(double turnover){
    balance = balance+turnover;
    System.out.println(name+"您的账户已存入"+balance+"元"+"当前余额为:"+balance+"元");
}

//取款方法
public void withdrawal(String password,double turnover)
{
    if(this.password!=password)
    {
        System.out.println("密码错误,重新输入");
        return;
    }
    if(balance-turnover>=0)
    {
        System.out.println("您已取出"+turnover+"元"+"当前余额为"+(balance-turnover));
    }
    else{
        System.out.println("余额不足");
    }       
}

static void welcomenext()
{
    System.out.println("请恁鞋带好随身物品,欢迎下次再来");
}

}

public class Trade{
public static void main(String[] args){
Bank.bankName="建设银行";
Bank.welcome();
Bank bank = new Bank("冯润发","123456",200.0);

    bank.depoist(200.0);
    bank.withdrawal("123456",150.0);
    bank.withdrawal("654321",150.0);
    bank.withdrawal("123456",450.0);
    Bank.welcomenext();
}

}
···

员工类
···
package shiyan4;

public class Employee { //定义成员变量
private String empId;
private String name;
private int age;
private String sex;
//定义构造方法
public Employee(){}
//定义成员方法set,get方法
public String getEmpId(){
return empId;
}
public void setEmpId(String empId){
this.empId=empId;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age=age;
}
public String getSex(){
return sex;
}
public void setSex(String sex){
this.sex=sex;
}
//定义成员方法show方法
public void show(){
System.out.println("员工编号是:"+getEmpId()+"\t姓名是:"+getName()+"\t年龄:"+getAge()+"\t性别:"+getSex());
}

}

package shiyan4;

public class Test {
public static void main(String[] args){
//创建对象
Employee e = new Employee();
e.setEmpId("河北农业大学");
e.setName("冯润发");
e.setAge(21);
e.setSex("男");
//调用显示信息方法
e.show();
}

}
···

JAVA作业三

标签:显示   公司   输出   show   创建   bre   sys   password   with   

原文地址:https://www.cnblogs.com/frf123456/p/8886477.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!