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.

410 lines
7.6 KiB

12 months ago
/********* begin *********/
Cat a = new Cat();
a.setName("大花猫");
a.setAge(6);
a.voice();
a.eat();
a.show();
/********* end *********/
// 实例化一个Dog对象设置属性name和age调用voice()和eat()方法,再打印出名字和年龄信息
/********* begin *********/
Dog b = new Dog();
b.setName("大黑狗");
b.setAge(8);
b.voice();
b.eat();
b.show();
/********* end *********/
}
}
class Animal {
/********* 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 show() {
System.out.println(name+age+"岁");
}
/********* end *********/
}
class Cat extends Animal {
// 定义Cat类的voice()和eat()方法
/********* begin *********/
public void voice() {
System.out.println(super.getName()+"喵喵叫");
}
public void eat() {
System.out.println(super.getName()+"吃鱼");
}
/********* end *********/
}
class Dog extends Animal {
// 定义Dog类的voice()和eat()方法
/********* begin *********/
public void voice() {
System.out.println(super.getName()+"汪汪叫");
}
public void eat() {
System.out.println(super.getName()+"吃骨头");
}
/********* end *********/
package case3;
public class superTest {
public static void main(String[] args) {
// 实例化一个Student类的对象s为Student对象s中的school赋值打印输出信息
/********* begin *********/
Student s=new Student("张三",18);
s.school="哈佛大学";
s.print();
/********* end *********/
}
}
class Person {
/********* begin *********/
String name;
int age;
Person(String name,int age){
this.name=name;
this.age=age;
}
/********* end *********/
}
class Student extends Person {
/********* begin *********/
Student(String name,int age){
super(name,age);
}
String school;
void print(){
System.out.println("姓名:"+name+",年龄:"+age+",学校:"+school);
}
/********* end *********/
}
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 *********/
public String name;
public int age;
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;
}
void talk(){
System.out.println("我是:"+name+",今年:"+age+"岁,我在"+school+"上学");
}
/********* end *********/
}
package case5;
public class abstractTest {
public static void main(String[] args) {
/********* begin *********/
// 分别实例化Student类与Worker类的对象并调用各自构造方法初始化类属性。
Student s=new Student("张三",20,"学生");
Worker w=new Worker("李四",30,"工人");
s.talk();
w.talk();
// 分别调用各自类中被复写的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 *********/
Student(String name,int age,String occupation){
this.name=name;
this.age=age;
this.occupation=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){
this.name=name;
this.age=age;
this.occupation=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();
System.out.println(s.talk());
/********* end *********/
}
}
// 声明一个Person接口并在里面声明三个常量name、age和occupation并分别赋值声明一抽象方法talk()
interface Person {
/********* begin *********/
final String name="张三";
final int age=18;
final String occupation="学生";
public abstract String talk();
/********* end *********/
}
// Student类继承自Person类 复写talk()方法返回姓名、年龄和职业信息
class Student implements Person {
/********* begin *********/
public String talk() {
return "学生——>姓名:" + this.name + ",年龄:" + this.age + ",职业:"
+ this.occupation + "";
}
/********* end *********/
}
package case8;
public class TestPolymorphism {
public static void main(String[] args) {
// 以多态方式分别实例化子类对象并调用eat()方法
/********* begin *********/
Animal a=new Dog();
a.eat();
Animal b=new Cat();
b.eat();
Animal c=new Lion();
c.eat();
/********* 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 *********/
}