参考答案:
复制代码
package case1;
public class TestPersonDemo {
public static void main(String[] args) {
/********* begin *********/
// 声明并实例化一Person对象p
Person p = new Person();
// 给p中的属性赋值
p.setName("张三");
p.setAge(18);
// 调用Person类中的talk()方法
p.talk();
/********* end *********/
}
}
// 在这里定义Person类
class Person {
/********* begin *********/
private String name;
private int age;
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;
}
void talk() {
System.out.println("我是:" + name + ",今年:" + age + "岁");
}
/********* end *********/
}
复制代码
代码展示:
复制代码
package case2;
public class extendsTest {
public static void main(String args[]) {
// 实例化一个Cat对象,设置属性name和age,调用voice()和eat()方法,再打印出名字和年龄信息
/********* begin *********/
Cat cat= new Cat();
cat.name="大花猫";
cat.age=6;
cat.voice();
cat.eat();
System.out.println(cat.name+cat.age+"岁");
/********* end *********/
// 实例化一个Dog对象,设置属性name和age,调用voice()和eat()方法,再打印出名字和年龄信息
/********* begin *********/
Dog dog= new Dog();
dog.name="大黑狗";
dog.age=8;
dog.voice();
dog.eat();
System.out.println(dog.name+dog.age+"岁");
/********* end *********/
}
}
class Animal {
/********* begin *********/
protected String name;
protected int age;
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 ;
}
/********* end *********/
}
class Cat extends Animal {
// 定义Cat类的voice()和eat()方法
/********* begin *********/
public void voice(){
System.out.println(name+"喵喵叫");
}
public void eat(){
System.out.println(name+"吃鱼");
}
/********* end *********/
}
class Dog extends Animal {
// 定义Dog类的voice()和eat()方法
/********* begin *********/
public void voice(){
System.out.println(name+"汪汪叫");
}
public void eat(){
System.out.println(name+"吃骨头");
}
/********* end *********/
}
复制代码
代码展示:
复制代码
package case3;
public class superTest {
public static void main(String[] args) {
// 实例化一个Student类的对象s,为Student对象s中的school赋值,打印输出信息
/********* begin *********/
Person p= new Person();
p.setName("张三");
p.setAge(18);
p.talk();
Student stu= new Student();
stu.school="哈佛大学";
System.out.println(",学校:"+stu.school);
/********* end *********/
}
}
class Person {
/********* begin *********/
private String name ;
private int age;
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 void talk(){
System.out.print("姓名:"+name+",年龄:"+age);
}
/********* end *********/
}
class Student extends Person {
/********* begin *********/
public String school;
public String getSchool(){
return school;
}
public void setSchool(String name){
this.school= school;
}
/********* end *********/
}
复制代码
代码展示:
复制代码
package case4;
public class overridingTest {
public static void main(String[] args) {
// 实例化子类对象s,调用talk()方法打印信息
/********* begin *********/
Student stu=new Student();
stu.talk("张三",18,"哈佛大学");
/********* end *********/
}
}
class Person {
/********* begin *********/
String name;
int age;
protected void talk(String name, int age){
this.name=name;
this.age=age;
System.out.print("我是:"+this.name+",今年:"+this.age);
}
/********* end *********/
}
class Student extends Person {
/********* begin *********/
String school;
public void talk(String name,int age,String school){
super.talk("张三",18);
this.school=school;
System.out.println("岁,我在"+this.school+"上学");
}
/********* end *********/
}
复制代码
代码展示:
复制代码
package case5;
public class abstractTest {
public static void main(String[] args) {
/********* begin *********/
// 分别实例化Student类与Worker类的对象,并调用各自构造方法初始化类属性。
Student stu=new Student("张三",20,"学生");
Worker wor=new Worker("李四",30,"工人");
// 分别调用各自类中被复写的talk()方法 打印信息。
stu.talk();
wor.talk();
/********* end *********/
}
}
// 声明一个名为Person的抽象类,在Person中声明了三个属性name age occupation和一个抽象方法——talk()。
abstract class Person {
/********* begin *********/
protected String name;
protected int age;
protected String occupation;
Person(String name,int age,String occupation){
this.name=name;
this.age=age;
this.occupation=occupation;
}
abstract void talk();
/********* end *********/
}
// Student类继承自Person类,添加带三个参数的构造方法,复写talk()方法 返回姓名、年龄和职业信息
class Student extends Person {
/********* begin *********/
Student(String name,int age,String occupation) {
super(name,age,occupation);
}
void talk(){
System.out.println("学生——>姓名:"+name+",年龄:"+age+",职业:"+ occupation+"!");
}
/********* end *********/
}
// Worker类继承自Person类,添加带三个参数的构造方法,复写talk()方法 返回姓名、年龄和职业信息
class Worker extends Person {
/********* begin *********/
Worker(String name,int age,String occupation) {
super(name,age,occupation);
}
void talk(){
System.out.println("工人——>姓名:"+name+",年龄:"+age+",职业:"+occupation+"!");
}
/********* end *********/
}
复制代码
代码展示:
复制代码
package case6;
public class finalTest {
public static void main(String args[]) {
Bike1 obj = new Bike1();
obj.run();
Honda honda = new Honda();
honda.run();
Yamaha yamaha = new Yamaha();
yamaha.run();
}
}
//不可以修改 final 变量的值
// final方法,不可以重写
不可以扩展 final 类
class Bike1 {
int speedlimit = 90;
void run() {
speedlimit = 120;
System.out.println("speedlimit=120");
}
}
class Bike2 {
void run() {
System.out.println("running");
}
}
class Honda extends Bike2 {
void run() {
System.out.println("running safely with 100kmph");
}
}
class Bike3 {
}
class Yamaha extends Bike3 {
void run() {
System.out.println("running safely with 100kmph");
}
}
复制代码
代码展示:
复制代码
package case7;
public class interfaceTest {
public static void main(String[] args) {
// 实例化一Student的对象s,并调用talk()方法,打印信息
/********* begin *********/
Student s =new Student();
s.talk();
/********* end *********/
}
}
// 声明一个Person接口,并在里面声明三个常量:name、age和occupation,并分别赋值,声明一抽象方法talk()
interface Person {
/********* begin *********/
String name="张三";
int age=18;
String occupation="学生";
abstract void talk();
/********* end *********/
}
// Student类继承自Person类 复写talk()方法返回姓名、年龄和职业信息
class Student implements Person {
/********* begin *********/
public void talk(){
System.out.println("学生——>姓名:"+name+",年龄:"+age+",职业:"+occupation+"!");
}
/********* end *********/
}
复制代码
代码展示:
package case8;
public class TestPolymorphism {
public static void main(String[] args) {
// 以多态方式分别实例化子类对象并调用eat()方法
/********* begin *********/
show(new Dog());
show(new Cat());
show(new Lion());
}
public static void show(Animal a){
a.eat();
if(a instanceof Dog){
}else if(a instanceof Cat){
}else{
}
}
/********* end *********/
}
// Animal类中定义eat()方法
abstract class Animal {
/********* begin *********/
abstract void eat();
/********* end *********/
}
// Dog类继承Animal类 复写eat()方法
class Dog extends Animal {
/********* begin *********/
public void eat() {
System.out.println("eating bread...");
}
/********* end *********/
}
// Cat类继承Animal类 复写eat()方法
class Cat extends Animal {
/********* begin *********/
public void eat() {
System.out.println("eating rat...");
}
/********* end *********/
}
// Lion类继承Animal类 复写eat()方法
class Lion extends Animal {
/********* begin *********/
public void eat() {
System.out.println("eating meat...");
}
/********* end *********/
}
复制代码