可以在子类的method.in java中调用超类方法吗? [英] Can a superclass method be called inside the subclass's method.in java?

查看:76
本文介绍了可以在子类的method.in java中调用超类方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/ *实现多重继承.... * /



/* Implementing multiple inheritance....*/

class Student
{
	int rollNo;
	void getNumber(int n)
	{
		 rollNo=n;
	}
	
	void putNumber()
	{
		System.out.println("Roll no = "+rollNo);
	}
	
}

class Test extends Student
{
	float part1,part2;
	void getMarks(float x,float y)
	{
		part1=x;
		part2=y;
	}
	void putMarks()
	{
		System.out.println("Marks obtained:- ");
		System.out.println("part1 = "+part1);
		System.out.println("part2 = "+part2);
	}
}

interface Sports
{
	static final float sport_wt=6.0f;
	void put_wt();
}

class Results extends Test implements Sports		//multiple inheritance
{
	float total;
	public void put_wt()
	{
		System.out.println("Sports weightage = "+sport_wt);
	}
	
	void display()
	{
		total=part1+part2+sport_wt;
		putNumber();
		putMarks();
		put_wt();
		System.out.println("final result = "+total);
		
	}
	
}

class MultipleInheritance
{
	public static void main(String args[])
	{
		Results r=new Results();
		r.getNumber(33);
		r.getMarks(99.99f,99.99f);
		r.display();
	}	
}





我的尝试:



你可以在程序中看到Result类中的display()方法是直接调用方法putnumber()和putmarks()以及putWt而不创建对象而我认为只有静态方法可以直接由类调用....用于调用非静态方法,你必须创建和对象......虽然程序运行完全正常...所以如果我能得到一个答案,它真的会有所帮助,谢谢提前...



What I have tried:

as you can see in the program that the display() method in Result class is directly calling methods putnumber() and putmarks() and also putWt without creating objects and i thought that only static methods can be called directly by class....for calling non static methods you must create and object......although the program runs perfectly fine...so it would really be helpful if i could get an answer and thanks in advance...

推荐答案

您创建了<$> c 实例 r (所以不需要提及静态方法)。现在,通过继承, r 学生,它可以调用学生的任何方法class提供。
You created an instance of Results, r (so no need to mention static methods). Now, by inheritance, r is a Student and it can invoke any method the Student class provides.


这篇关于可以在子类的method.in java中调用超类方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆