【问题标题】:What is a supertype method?什么是超类型方法?
【发布时间】:2013-02-28 07:41:49
【问题描述】:

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

【问题讨论】:

  • 你的意思是超类的方法
  • 如果我没记错的话,那就是使用super 调用已被另一个扩展的类的方法,例如super.onCreate 调用Activity classonCreate 方法
  • 您能详细说明一下吗?
  • 你先搞清楚面向对象编程了吗?如果没有,我建议从那个开始。
  • 在 JAVA GENERICS 中使用超类型。

标签: java oop supertype


【解决方案1】:

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

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

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

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

class A {
   void set()
}

Set 是类B 的超类型方法。

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

class C {
    void set()        
}

那么set()方法对于C类来说是not supertype,因为类A和类C之间没有关系(关系是由extends关键字创建的,用于继承)。

【讨论】:

  • 此外,接口也是实现它的任何类(以及扩展它的任何接口)的超类型。这也应该使接口默认方法成为超类型方法。
【解决方案2】:

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

  1. 使用公共方法创建一个类,例如printSomething()
    public void printSomething() {
       System.out.println("hello, I am the first class");
     }
    
  2. 创建从第一个类继承的第二个类并覆盖 printSomething 方法
    @override
    public void printSomething() {
    super.printSomething();
    }
    
  3. 编写一个小程序,调用类二的方法printSomething,看看会发生什么

【讨论】:

    【解决方案3】:

    构造函数级别的超级

    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();
        }
    }
    

    【讨论】:

      【解决方案4】:

      在java中每件事都是对象,方法也是类java.lang.reflect.Method的对象 所以方法的超类可以认为是java.lang.reflect.Method的超类,即AccessibleObject

      【讨论】:

        【解决方案5】:

        超类型和子类型是继承的属性,即为了代码的可重用性。我给你举了超级类和子类的例子。更多内容可以关注here

        使用系统;

        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

        【讨论】:

        • 您好,您似乎正在添加一个链接,该链接指向一个网站,该网站的广告没有披露隶属关系。您似乎正试图在该网站上获得更多浏览量。见How to not be a spammer。请注意,这样做可能会增加社区成员的怀疑,从而可能导致您的帐户被暂停。请参阅此meta answer。我写这篇评论是为了事先警告你。问候
        【解决方案6】:

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

        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();
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2023-01-22
          • 2021-08-12
          • 2014-04-28
          • 2019-10-11
          • 2018-05-09
          • 2015-12-23
          • 1970-01-01
          • 2012-10-31
          • 2018-07-20
          相关资源
          最近更新 更多