java 什么是超类型方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15130067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:35:13  来源:igfitidea点击:

What is a supertype method?

javaoopsupertype

提问by ray

I have googled couple of times but still can't understand the supertype method. Can anyone please explain what is this?

我已经用谷歌搜索了几次,但仍然无法理解超类型方法。谁能解释一下这是什么?

回答by navyad

There is a notion of supertype and subtype in OOPS, In java this kind of relationship is implemented by inheritance i.e. using extendskeyword:

OOPS中有超类型和子类型的概念,在java中这种关系是通过继承实现的,即使用extends关键字:

class A {} // super class
class B extends A {} //sub class

Any member (fields, methods) declared in super class is to be called supertype.

超类中声明的任何成员(字段、方法)都称为超类型。

Therefore in above context if class Ahas method like

因此,在上述上下文中,如果类A具有类似的方法

class A {
   void set()
}

Set is supertype method for class B.

Set 是 class 的超类型方法B

However, notice that if there is another class say C:

但是,请注意,如果有另一个类说C

class C {
    void set()        
}

Then set()method is not supertypefor Cclass because there is no relationship between class Aand class C(relationship is created by extendskeyword, for inheritance).

然后set()方法不是C类的超类型,因为类A和类之间没有关系C(关系是通过extends关键字创建的,用于继承)。

回答by Karthik reddy

Super at Constructer level

超级在构造器级别

    class SuperClass
{
    int num=10;
    public void display()
    {
        System.out.println("Superclass display method");
    }
}
class SubClass extends SuperClass
{
    int num=20;

    public void display()
    {
        System.out.println("the value of subclass variable name:"+num);
        System.out.println("Subclass display method");
    }
        public void Mymethod()
        {
            super.display();
            System.out.println("the value of superclass variable name:"+super.num);
        }
        public static void main(String[] args)
        {
            SubClass obj=new SubClass();
            obj.Mymethod();
            obj.display();
        }
}

回答by Krushna

In java every thing are object and a method is also a object of class java.lang.reflect.Method So the super type of method can be consider as the super class of java.lang.reflect.Methodthat is the AccessibleObject.

在java中,一切都是对象,方法也是类java.lang.reflect.Method的对象所以方法的超类型可以被认为java.lang.reflect.MethodAccessibleObject.

回答by Michael Meyer

if you are talking about calling a super method, you should try the following

如果您正在谈论调用超级方法,则应尝试以下操作

  1. create a class with a method public method e.g. printSomething()

    public void printSomething() { System.out.println("hello, I am the first class"); }

  2. create a second class which inherites from the first class and override the printSomething method

    @override public void printSomething() { super.printSomething(); }

  3. write a small programm which call the method printSomething of class two and see what will happen

  1. 创建一个具有方法公共方法的类,例如 printSomething()

    public void printSomething() { System.out.println("你好,我是头等舱"); }

  2. 创建从第一个类继承的第二个类并覆盖 printSomething 方法

    @override public void printSomething() { super.printSomething(); }

  3. 编写一个小程序,调用第二类的printSomething方法,看看会发生什么

回答by Anup Singh

Super Type and sub type is a property of inheritance i.e for the purpose of re-usability of code. I am giving you example of Super Class and Sub class. For more you can follow here.

超级类型和子类型是继承的属性,即为了代码的可重用性。我正在给你超级类和子类的例子。有关更多信息,您可以在这里关注

using System;

使用系统;

namespace MultilevelInheritance
{
    public class Customer
    {
        public float fDis { get; set; }
        public Customer()
        {
            Console.WriteLine("I am a normal customer.");
        }
        public virtual void discount()
        {
            fDis = 0.3F;
            Console.WriteLine("Discount is :{0}", fDis);
        }

    }
    public class SilverCustomer : Customer
    {
        public SilverCustomer()
            : base()
        {
            Console.WriteLine("I am silver customer.");
        }
        public override void discount()
        {
            fDis = 0.4F;
            Console.WriteLine("Discount is :{0}", fDis);
        }
    }
    class GoldenCustomer : SilverCustomer
    {
        public GoldenCustomer()
        {
            Console.WriteLine("I am Golden customer.");
        }
        public override void discount()
        {
            fDis = 0.6F;
            Console.WriteLine("Discount is :{0}", fDis);
        }

    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MultilevelInheritance
{
    class Program
    {
        static void Main(string[] args)
        {
            Customer objCus = new Customer();
            objCus.discount();

            SilverCustomer objSil = new SilverCustomer();
            objSil.discount();

            GoldenCustomer objGold = new GoldenCustomer();
            objGold.discount();


            Console.ReadLine();
        }
    }
}

enter image description here

在此处输入图片说明

回答by Karthik reddy

Super is used to invoke parent class Properties used at 3 levels variable constructer and method level

Super用于调用父类Properties,用于3级变量构造器和方法级

1.Super at Variable

1.超变

class Super
{
    int age;
    Super(int age)
    {
        this.age=age;
    }
        public void getAge()
        {
            System.out.println(age);
        }
}
class Sub extends Super
{
    Sub(int age)
    {
        super(age);
    }
    public static void main(String[] args)
    {
         Super obj=new  Super(24);
         obj.getAge();
    }
}