JAVA 子类与继承例题


子类的继承性

public class People {
   int age,leg=2,hand=2;
   protected void showPeoleMess(){
	   System.out.printf("%d岁,%d只脚,%d只脚\t",age,leg,hand);
}
}

public class Student extends People {
     int number;
     void tellNumber(){
    	 System.out.printf("学号:%d\t",number);
     }
     int add(int x,int y){
    	 return x+y;
     }
}

public class UniverStudent extends Student {
     int multi(int x,int y){
    	 return x*y;
     }
}

public class Exam {

	public static void main(String[] args) {
	  Student zhang = new Student();
	  zhang.age=17;   //访问继承的成员变量
	  zhang.number=100101;
	  zhang.showPeoleMess();  //调用继承的成员变量
	  zhang.tellNumber();
	  int x=9,y=29;
	  System.out.println("会做加法");
	  int result=zhang.add(x, y);
	  System.out.printf("%d+%d=%d\n",x,y,result);
	  UniverStudent geng = new UniverStudent();
	  geng.age=21;//访问继承的成语变量
	  geng.number=6609;//访问继承的成语变量
      geng.showPeoleMess();//调用继承的成员变量
      geng.tellNumber();//调用继承的成员变量
      System.out.print("会做加法");
      result=geng.add(x, y); //调用继承的成员变量
      System.out.printf("%d+%d=%d\t",x,y,result);
      System.out.print("会做乘法");
      result=geng.multi(x, y); //调用继承的成员变量
      System.out.printf("%d*%d=%d\t",x,y,result);
	}

}

子类与对象

class People{
	private int averHeight=166;
	public int  getAverHeight() {
		return averHeight;
	}
}
class ChinaPeople extends People{
	int height;
	public void setHeight(int h) {
		//height=h+averHeight;  非法,子类没有继承averHeight
		height=h;
	}
	public int getHeight(){
		return height;
	}
}
public class exam {
	public static void main(String[] args) {
		ChinaPeople zhangsan=new ChinaPeople();
		System.out.println("子类对象未继承的averageHeight的值是:"+zhangsan.getAverHeight());
		zhangsan.setHeight(178);
		System.out.println("子类对象的实例变量height的值是:"+zhangsan.getHeight());

	}

}

成员变量的隐藏


public class Goods {
    public double weight;
    public void oldSetWeight(double w){
    	weight=w;
    	System.out.println("double型的weight="+weight);
    }
    public double oldGetPrice(){
    	double price=weight*10;
    	return price;
    }
}
public class CheapGoods extends Goods{
    public int weight;
    public void newSetWeight(int w){
    	weight=w;
    	System.out.println("int型的weight="+weight);
    }
    public double newGetPrice(){
    	double price=weight*10;
    	return price;
    }
}
public class Exam {

	public static void main(String[] args) {
            CheapGoods cheapGoods=new CheapGoods();
            //cheapGoods.weight=198.98; 是非法的,因为子类对象的weight变量已经是int型
            cheapGoods.newSetWeight(198);
            System.out.println("对象cheapGoods的weight的值是:"+cheapGoods.weight);
            System.out.println("cheapGoods用子类新增的优惠方法计算价格:"+cheapGoods.newGetPrice());
            cheapGoods.oldSetWeight(198.987);//子类对象调用继承的方法操作隐藏的double型变量weight;
            System.out.println("cheapGoods使用继承的方法(无优惠)价格计算:"+cheapGoods.oldGetPrice());
            
	}

}

方法重写

public class University {
    void enterRule(double math,double english,double chinese){
    	double total=math+english+chinese;
    	if(total>=80)
    		System.out.println(total+"分数线达到大学录取线");
    	else
    		System.out.println(total+"分数线未达到大学录取线");
    }
}
public class ImportantUniversity {
	 void enterRule(double math,double english,double chinese){
	    	double total=math+english+chinese;
	    	if(total>=220)
	    		System.out.println(total+"分数线达到重点大学录取线");
	    	else
	    		System.out.println(total+"分数线未达到重点大学录取线");
	    }
}
public class exam {

	public static void main(String[] args) {
           double math=62,english=76.5,chinese=67;
           ImportantUniversity univer=new ImportantUniversity();
           univer.enterRule(math, english, chinese); //调用重写的方法
           math=91;
           english=82;
           chinese=86;
           univer.enterRule(math, english, chinese);//调用重写的方法
           
	}

}



package hah;
class A{
	float computer(float x,float y){
		return x+y;
	}
	public int g(int x,int y){
		return x+y;
	}
}
class B extends A{
	float computer(float x,float y){
		return x*y;
	}
}
public class exam {

	public static void main(String[] args) {
		B b=new B();
		double result = b.computer(8, 9);//b调用重写的方法
		System.out.println(result);
		int m=b.g(12, 8);  //b调用继承的方法
		System.out.println(m);  
	}
}

super关键字

class Sum{
	int n;
	float f(){
		float sum=0;
		for(int i=1;i<=n;i++)
			sum=sum+i;
		return sum;
	}
}
class Average extends Sum{
	int n;
	float f(){
		float c;
		super.n=n;
		c=super.f();
		return c/n;
	}
	float g(){
		float c;
		c=super.f();
		return c/2;
	}
}
public class exam {
	public static void main(String[] args) {
          Average aver=new Average();
          aver.n=100;
          float resultOne=aver.f();
          float resultTwo=aver.g();
          System.out.println("resultOne="+resultOne);
          System.out.println("resultTwo="+resultTwo);
	}

}
以下是一个关于Java封装、继承和多态的例子: ```java // 父类Animal public class Animal { private String name; public Animal(String name) { this.name = name; } public String getName() { return name; } public void eat() { System.out.println(name + " is eating."); } } // 子类Dog public class Dog extends Animal { public Dog(String name) { super(name); } public void bark() { System.out.println(getName() + " is barking."); } } // 子类Cat public class Cat extends Animal { public Cat(String name) { super(name); } public void meow() { System.out.println(getName() + " is meowing."); } } // 测试类 public class Test { public static void main(String[] args) { Animal animal1 = new Dog("Lucky"); Animal animal2 = new Cat("Tom"); animal1.eat(); animal2.eat(); // animal1.bark(); // 编译错误,因为Animal类没有bark()方法 // animal2.meow(); // 编译错误,因为Animal类没有meow()方法 Dog dog = (Dog) animal1; Cat cat = (Cat) animal2; dog.bark(); cat.meow(); } } ``` 在这个例子中,Animal类是一个父类,它有一个私有的name属性和一个公共的eat()方法。Dog和Cat类是Animal类的子类,它们继承了父类的属性和方法,并且分别有自己的方法bark()和meow()。 在测试类中,我们首先创建了一个Dog对象和一个Cat对象,并且调用它们的eat()方法。由于Animal类没有bark()和meow()方法,我们不能直接调用这些方法。但是我们可以将animal1和animal2分别强制转换为Dog和Cat类型的对象,这样就可以调用它们的特有方法。 这个例子展示了Java中封装、继承和多态的特性。封装指的是将属性和方法封装在类中,只对外暴露必要的接口。继承指的是子类继承父类的属性和方法,并且可以在此基础上添加自己的属性和方法。多态指的是同一个方法在不同的对象中有不同的实现方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值