1 this关键字
package com.Hani; class E{ private F b=null; public void fun(){ this.b=new F (this); this.b.fun(); } public void print(){ System.out.println("Hello World!!!"); } }; class F{ private E a=null; public F(E a){ this.a=a; } public void fun(){ this.a.print(); } }; public class DemoThis { public static void main(String args[]){ new E().fun(); } }
2 父类
package com.Hani; abstract class A{ public abstract void fun(); } class B extends A{ public void fun(){ System.out.println("Hello World!"); } } public class DemoAbstract{ public static void main(String args[]){ B b=new B(); b.fun(); } }
3.
package com.Hani; abstract class C{ private String name; public C(String name){ this.name=name; } public String getName(){ return this.name; } public abstract void fun(); }; class D extends C { public D(String name){ super(name); } public void fun() { System.out.println(super.getName()); } }; public class DemoAbstrsct1 { public static void main(String args[]){ D d=new D("爱豆鹿晗"); d.fun(); } }
4.工人学生
public abstract class Person { private String name; private int age; public Person(String name,int age){ this.name=name; this.age=age; } public Person() { super(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public abstract String speak(); public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
public class Student extends Person { private float score; public Student(){ } public Student(String name,int age,float score) { // TODO 自动生成的构造函数存根 super(name,age); this.score=score; //this.setScore(score); } @Override public String speak() { // TODO 自动生成的方法存根 return "我是"+super.getName()+", 我"+super.getAge()+"岁了,"+"我的成绩是"+this.score; } public float getScore() { return score; } public void setScore(float score) { this.score = score; } }
public class Worker extends Person { private float rewards; public Worker(String name, int age, float rewards) { // TODO 自动生成的构造函数存根 super(name, age); this.rewards=rewards; } @Override public String speak() { // TODO 自动生成的方法存根 return "我是"+super.getName()+", 我"+super.getAge()+"岁了,"+"我的工资是"+this.rewards; } public float getRewards() { return rewards; } public void setRewards(float rewards) { this.rewards = rewards; } }
package cn.wzx.www; public class DemoWoSt { public static void main(String[] args) { // TODO 自动生成的方法存根 Person student =new Student("张三",18,59.0f); Person worker =new Worker("李四",30,3600.8f); print(student.speak()); print(worker.speak()); } private static void print(String speak) { System.out.println(speak); } }
5.抽象类
package cn.dede.w;
abstract class A3
{
public abstract void fun();
}
class B3 extends A3
{
public void fun()
{
System.out.println("yeah");
}
}
public class AbstractDemo3
{
public static void main(String args[])
{
A3 a=new B3();
a.fun();
}
}