1.
(继承、this
和
super
关键字)有以下代码
class Super{
public Super(){
System.out.println("Super()");
}
public Super(String str){
System.out.println("Super(String)");
}
}
class Sub extends Super{
public Sub(){
System.out.println("Sub()");
}
public Sub(int i){
this();
System.out.println("Sub(int)");
}
public Sub(String str){
super(str);
System.out.println("Sub(String)");
}
}
public class TestSuperSub{
public static void main(String args[]){
Sub s1 = new Sub();
Sub s2 = new Sub(10);
Sub s3 = new Sub("hello");
}
}
写出该程序运行的结果。
输出结果:
Super()
Sub()
Super()
Sub()
Sub(int)
Super(String)
Sub(String)
2.
(super
)看下面代码,写出程序运行的结果
class Super{
public void m1(){
System.out.println("m1() in Super" );
}
public void m2(){
System.out.println("m2() in Super" );
}
}
class Sub extends Super{
public void m1(){
System.out.println("m1() in Sub");
super.m1();
}
}
public class TestSuperSub{
public static void main(String args[]){
Sub s = new Sub();
s.m1();
s.m2();
}
}
输出结果:
m1() in Sub
m1() in Super
m2() in Super
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 Sandwichextends 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 Super{
int method(){return 0;}
}
class Sub extends Super{
// 1
}
在
//1
处,能编译通过的代码为:
A. public int method(){return 0;}
B. void method(){}
C. void method(int n){}
AC
5. *
(方法覆盖)有如下代码
class Super{
private void method(){}
}
class Sub extends Super{
//1
}
在
//1
处,能编译通过的代码为:
A. public int method(){return 0;}
B. void method(){}
C. void method(int n){}
D. private void method(){}
6.
package day14;
public class Demo14_3 {
public static void main(String[] args) {
Student stu=new Student("张三",23,"江苏","214000","1381402222");
System.out.println(stu.getName());
System.out.println(stu.getAge());
System.out.println(stu.getAdress());
System.out.println(stu.getZipCode());
System.out.println(stu.getMobile());
stu.getPostAddress();
}
}
class Student{
private String name;
private int age;
private String adress;
private String zipCode;
private String mobile;
public Student(String name,int age,String adress,String zipCode,String mobile){
this.name=name;
this.age=age;
this.adress=adress;
this.zipCode=zipCode;
this.mobile=mobile;
}
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 getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public void getPostAddress(){
System.out.println(adress+" "+zipCode);
}
}
class Account{
private int id;
private double balance;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String getPassword() {
return null;
}
public void setPassword(String password) {
if(password.length()!=6){
return;
}
this.password = password;
}
}
class SaveAccount extends Account{
private double interestRate;
public double getInterestRate() {
return interestRate;
}
public void setInterestRate(double interestRate) {
if(interestRate>0 && interestRate<10/100){
this.interestRate = interestRate;
}
}
}
class CreditAccount extends Account{
private double creditLine;
}
8
、描述不同的动物不同的叫法
1
:定义动物类
有名字,有吃和叫的方法
2
:定义狗继承动物重写父类吃和叫的方法
3
:定义猫继承动物重写父类吃和叫的方法
要求:通过主函数调用相关函数和属性信息信息
class Animal {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void eat(){
System.out.println(name+"吃東西");
}
public void call(){
System.out.println(name+"在叫...");
}
}
class Dog extends Animal{
@Override
public void eat() {
System.out.println(super.getName()+"在吃東西");
}
@Override
public void call() {
System.out.println(super.getName()+"在叫....");
}
}
class Cat extends Animal{
@Override
public void eat() {
System.out.println(super.getName()+"在吃東西");
}
@Override
public void call() {
System.out.println(super.getName()+"在叫....");
}
}
public class TestAnimal{
public static void main(String[] args) {
Animal a1 = new Dog();
a1.setName("夠");
a1.eat();
a1.call();
System.out.println();
Animal a2 = new Dog();
a2.setName("貓");
a2.eat();
a2.call();
}
}
9
、某公司的雇员分为以下若干类:
Employee
:这是所有员工总的父类,
属性:员工的姓名和生日月份。
方法:getSalary(int month)
根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励
100
元。
SalariedEmployee
:
Employee
的子类,拿固定工资的员工。
属性:月薪
HourlyEmployee
:
Employee
的子类,按小时拿工资的员工,每月工作超出
160
小时的部分按照
1.5
倍工资发放
属性:每小时的工资、每月工作的小时数
SalesEmployee
:
Employee
的子类,销售人员,工资由月销售额和提成率决定
属性:月销售额、提成率
BasePlusSalesEmployee
:
SalesEmployee
的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分
属性:底薪。
实现:1
、李易峰 固定员工 ,月薪
3000
,生日月份
2
月,工作
180
小时,获取工资多少
2
、张子明 小时工,每小时
25
,工作
180
小时,获取工资多少
3
、林肯 销售人员 月销售额
30
万,提成
0.3%
获取工资
4
、阳光 固定底薪销售人员 底薪
1500
,销售
20
万,提成
0.2%
获取工资
public class TestEmployee {
public static void main(String[] args) {
SalariedEmployee se = new SalariedEmployee();
se.setName("李易峰");
se.setSalary(3000);
se.setBirthMonth(2);
se.getSalary(2);
HourlyEmployee he = new HourlyEmployee();
he.setName("张子明");
he.setHourSalary(25);
he.setHours(180);
he.getSalary();
SalesEmployee sae = new SalesEmployee();
sae.setName("林肯");
sae.setMonthsales(300000);
sae.setRoyalRate(0.003);
sae.getSalary();
BasePlusSalesEmployee bse = new BasePlusSalesEmployee();
bse.setName("阳光");
bse.setBaseSalary(1500);
bse.setMonthsales(200000);
bse.setRoyalRate(0.002);
bse.getSalary();
}
}
class Employee {
String name;
int birthMonth;
double salary;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getBirthMonth() {
return birthMonth;
}
public void setBirthMonth(int birthMonth) {
this.birthMonth = birthMonth;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void getSalary(int month) {
if (birthMonth == month) {
System.out.println(name + "当月月薪" + (salary + 100));
}
}
}
class SalariedEmployee extends Employee {
}
class HourlyEmployee extends Employee {
double hourSalary;
double hours;
double salary;
public double getHourSalary() {
return hourSalary;
}
public void setHourSalary(double hourSalary) {
this.hourSalary = hourSalary;
}
public double getHours() {
return hours;
}
public void setHours(double hours) {
this.hours = hours;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void getSalary() {
if (hours > 160) {
System.out.println(name + "月薪为" + (hourSalary * 160 + ((hours - 160) * hourSalary * 1.5)));
} else {
System.out.println(name + "月薪为" + hourSalary * hours);
}
}
}
class SalesEmployee extends Employee {
double monthsales;
double royalRate;
public double getMonthsales() {
return monthsales;
}
public void setMonthsales(double monthsales) {
this.monthsales = monthsales;
}
public double getRoyalRate() {
return royalRate;
}
public void setRoyalRate(double royalRate) {
this.royalRate = royalRate;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void getSalary() {
System.out.println(name + "月薪为" + monthsales * royalRate);
}
}
class BasePlusSalesEmployee extends SalesEmployee {
double baseSalary;
public double getBaseSalary() {
return baseSalary;
}
public void setBaseSalary(double baseSalary) {
this.baseSalary = baseSalary;
}
public void getSalary() {
System.out.println(name + "月薪为" + baseSalary + monthsales * royalRate);
}
}