You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Java/Java类的继承与派生 - 封装、继承和多态.txt

391 lines
9.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

1.什么是封装,如何使用封装
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 String age;
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getAge(){
return age;
}
public void setAge(String age){
this.age = age;
}
public void talk(){
System.out.print("我是:"+name+",今年:"+age);
}
/********* end *********/
}
2.什么是继承,怎样使用继承
package case2;
public class extendsTest {
public static void main(String args[]) {
// 实例化一个Cat对象设置属性name和age调用voice()和eat()方法,再打印出名字和年龄信息
/********* begin *********/
Cat c=new Cat("大花猫",6);
c.voice("大花猫");
c.eat("大花猫");
System.out.println(c.getName()+c.getAge()+"岁");
/********* end *********/
// 实例化一个Dog对象设置属性name和age调用voice()和eat()方法,再打印出名字和年龄信息
/********* begin *********/
Dog d=new Dog("大黑狗",8);
d.voice("大黑狗");
d.eat("大黑狗");
System.out.println(d.getName()+d.getAge()+"岁");
/********* end *********/
}
}
class Animal {
/********* begin *********/
private String name;
private int age;
public Animal(String name,int age){
this.name = name;
this.age =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 Cat(String name,int age){
super(name,age);
}
public void voice(String name){
System.out.println(name+"喵喵叫");
}
public void eat(String name){
System.out.println(name+"吃鱼");
}
/********* end *********/
}
class Dog extends Animal {
// 定义Dog类的voice()和eat()方法
/********* begin *********/
public Dog(String name,int age){
super(name,age);
}
public void voice(String name){
System.out.println(name+"汪汪叫");
}
public void eat(String name){
System.out.println(name+"吃骨头");
}
/********* end *********/
}
3.方法的重写与重载
package case4;
public class overridingTest {
public static void main(String[] args) {
// 实例化子类对象s调用talk()方法打印信息
/********* begin *********/
Student s=new Student("张三",18,"哈佛大学");
s.talk();
/********* end *********/
}
}
class Person {
/********* begin *********/
String name;
int age;
public void talk(){
System.out.println("我是:"+name+"今年:"+age+"岁");
}
/********* end *********/
}
class Student extends Person {
/********* begin *********/
String school;
Student(String name,int age,String school){
this.name=name;
this.age=age;
this.school=school;
}
public void talk(){
System.out.print("我是:"+name+""+"今年:"+age+"岁"+""+"我在"+school+"上学");
}
/********* end *********/
}
4.抽象类
package case5;
public class abstractTest {
public static void main(String[] args) {
/********* begin *********/
// 分别实例化Student类与Worker类的对象并调用各自构造方法初始化类属性。
Student s=new Student("张三",20,"学生");
Worker w=new Worker("李四",30,"工人");
// 分别调用各自类中被复写的talk()方 印息
s.talk();
w.talk();
/********* end *********/
}
}
// 声明一个名为Person的抽象类在Person中声明了三个属性name age occupation和一个抽象方法——talk()。
abstract class Person {
/********* begin *********/
String name;
int age;
String occupation;
abstract void talk();
/********* end *********/
}
// Student类继承自Person类添加带三个参数的构造方法复写talk()方法 返回姓名、年龄和职业信息
class Student extends Person {
/********* begin *********/
public Student(String name,int age,String occupation){
this.name=name;
this.age=age;
this.occupation=occupation;
}
public void talk(){
System.out.println("学生——>姓名:"+this.name+""+"年龄:"+this.age+""+"职业:"+this.occupation+"");
}
/********* end *********/
}
// Worker类继承自Person类添加带三个参数的构造方法复写talk()方法 返回姓名、年龄和职业信息
class Worker extends Person {
/********* begin *********/
public Worker(String name,int age,String occupation){
this.name=name;
this.age=age;
this.occupation=occupation;
}
public void talk(){
System.out.println("工人——>姓名:"+this.name+""+"年龄:"+this.age+""+"职业:"+this.occupation+"");
}
/********* end *********/
}
5.接口
package case7;
public class interfaceTest {
public static void main(String[] args) {
// 实例化一Student的对象s并调用talk()方法,打印信息
/********* begin *********/
Student s=new Student();
System.out.println(s.talk());
/********* end *********/
}
}
// 声明一个Person接口并在里面声明三个常量name、age和occupation并分别赋值声明一抽象方法talk()
interface Person {
/********* begin *********/
final String name="张三";
final int age=18;
final String occupation="学生";
abstract String talk();
/********* end *********/
}
// Student类继承自Person类 复写talk()方法返回姓名、年龄和职业信息
class Student implements Person {
/********* begin *********/
public String talk(){
return "学生——>姓名:"+this.name+""+"年龄:"+this.age+""+"职业:"+this.occupation+"";
}
/********* end *********/
}
6.super关键字的使用
package case3;
public class superTest {
public static void main(String[] args) {
// 实例化一个Student类的对象s为Student对象s中的school赋值打印输出信息
/********* begin *********/
Student s = new Student();
s.school = "哈佛大学";
System.out.println("姓名:"+s.name+",年龄:"+s.age+",学校:"+s.school);
/********* end *********/
}
}
class Person {
/********* begin *********/
String name;
int age;
public Person(String name,int age){
this.name=name;
this.age=age;
}
/********* end *********/
}
class Student extends Person {
/********* begin *********/
String school;
public Student(){
super("张三",18); //super调用父类的方法
}
/********* end *********/
}
7.final关键字的理解与使用
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 类
//请在此添加你的代码
/********** Begin *********/
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");
}
}
/********** End **********/
8.什么是多态,怎么使用多态
package case8;
public class TestPolymorphism {
public static void main(String[] args) {
// 以多态方式分别实例化子类对象并调用eat()方法
/********* begin *********/
Animal dog=new Dog();
dog.eat();
Animal cat=new Cat();
cat.eat();
Animal lion=new Lion();
lion.eat();
/********* end *********/
}
}
// Animal类中定义eat()方法
class Animal {
/********* begin *********/
public 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 *********/
}