十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
第一个:
成都创新互联公司网站建设公司是一家服务多年做网站建设策划设计制作的公司,为广大用户提供了成都网站设计、做网站,成都网站设计,一元广告,成都做网站选成都创新互联公司,贴合企业需求,高性价比,满足客户不同层次的需求一站式服务欢迎致电。
public class Yaojing {
protected String name;
protected int age;
protected String gender;
public void showBasicInfo() {
System.out.println(toString());
}
public void eatTangSeng() {
System.out.println("吃饱了");
}
@Override
public String toString() {
return "Yaojing [name=" + name + ", age=" + age + ", gender=" + gender + "]";
}
}
第二个类
public class Zhizhujing extends Yaojing {
public void buildNet(){
System.out.println("蜘蛛在织网");
}
}
第三个类
public class Baigujing extends Yaojing {
public void beBeauty(){
System.out.println("白骨精");
}
}
看看下面这个例子,就会明白了:JAVA中继承可以实现代码复用,
由于在父类中已经定义的方法,被子类继承以后,就可以使用,实现了代码的复用
class Father{
private int moneyDollar=300;
int moneyHK=200;
int add(int x,int y){
return x+y;
}
}
class Son extends Father{
int moneyRMB=800;
public void changMoneyHK(int x){
moneyHK=x;
}
public void changMoneyRMB(int x){
moneyRMB=x;
}
int subs(int x,int y){
return x-y;
}
}
class GrandSon extends Son{
int multi(int x,int y){
return x*y;
}
}
public class Example5_1{
public static void main(String args[]){
int a=5,b=3;
Son son=new Son();
GrandSon sunzi=new GrandSon();
son.changMoneyHK(666);
son.changMoneyRMB(5000);
System.out.println("儿子的港币是继承的属性,当前的值是:"+son.moneyHK);
System.out.println("儿子的人民币是新增的属性,当前的值是:"+son.moneyRMB);
System.out.printf("减法是儿子新增的功能,%d-%d等于%d\n",a,b,son.subs(a,b));
System.out.printf("加法是儿子继承的功能,%d+%d等于%d\n",a,b,son.add(a,b));
System.out.println("孙子的港币和人民币都是继承的属性,,当前的值是:");
System.out.println("港币:"+sunzi.moneyHK+" 人民币:"+sunzi.moneyRMB);
System.out.printf("乘法是孙子新增的功能,%d*%d等于%d\n",a,b,sunzi.multi(a,b));
System.out.printf("加法是孙子继承的功能,%d+%d等于%d\n",a,b,sunzi.add(a,b));
System.out.printf("减法是孙子继承的功能,%d-%d等于%d\n",a,b,sunzi.subs(a,b));
}
}
我写了一个,内容比较简单的。代码如下:public class AnimalTest {
Animal animal;
public void eat(Animal animal){
animal.eat();
}
public void walk(Animal animal){
animal.walk();
}
public static void main(String args[]){
Animal animal=new Animal("animal");
Wolf w=new Wolf("wolf");
Goat g=new Goat("goat");
AnimalTest at=new AnimalTest();
at.eat(animal);
at.eat(w);
at.eat(g);
at.walk(animal);
at.walk(w);
at.walk(g);
}
}
class Animal {
String name;
public Animal(String name){
this.name=name;
}
public Animal(){}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void eat(){
System.out.println("animal eat");
}
public void walk(){
System.out.println("animal walk");
}
public String toString(){
return name;
}
}class Wolf extends Animal {
public Wolf(String name){
super(name);
}
public Wolf(){}
public void eat(){
System.out.println("wolf eat meat");
}
public void walk(){
System.out.println("wolf walk");
}
public String toString(){
return name;
}
}class Goat extends Animal {
public Goat(String name){
super(name);
}
public Goat(){}
public void eat(){
System.out.println("goat eat grass");
}
public void walk(){
System.out.println("goat walk");
}
public String toString(){
return name;
}
}
可运行的:
import java.awt.*;
import java.awt.event.*;
public class BackJFrame extends Frame{
public BackJFrame(){
super("台球");
setSize(300,300);
setBackground(Color.cyan); //背景
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing (WindowEvent e)
{System.exit(0);}
} );
}
public static void main(String args[]){
new BackJFrame();
}
}