Class Method

本文详细介绍了Java中Class与Method类的功能与使用方法。Class类用于表示运行中的Java应用程序中的类和接口,提供了获取类名、构造Class对象的方法。Method类则提供了关于类或接口上单一方法的信息及访问权限。
java.lang

Class Class<T>

  • Type Parameters:
    T - the type of the class modeled by thisClass object. For example, the type ofString.class isClass<String>. Use Class<?> if the class being modeled is unknown.
    All Implemented Interfaces:
    Serializable,AnnotatedElement,GenericDeclaration,Type


    public final class Class<T>
    extends Object
    implements Serializable, GenericDeclaration, Type, AnnotatedElement
    Instances of the class Classrepresent(表示) classes and interfaces in a running Java application. An enum is akind(种类) of class and an annotation(注释) is a kind of interface. Every array alsobelongs(属于) to a class that isreflected(反射) as aClass object that isshared(共享) by all arrays with the same element type and number ofdimensions(大小). Theprimitive(基本) Java types (boolean,byte,char,short, int, long, float, and double), and the keyword(关键字)void are also represented asClass objects.

    Class has no public cons tructor(构造方法). InsteadClass objects are constructedautomatically(自动的) by the JavaVirtual Machine(虚拟机) as classes are loaded and by calls to thedefineClass method in the class loader.

    The following example uses a Class object to print the class name of an object:

         void printClassName(Object obj) {
             System.out.println("The class of " + obj +
                                " is " + obj.getClass().getName());
         }
     

    It is also possible(可能的) to get theClass object for anamed(指定的) type (or for void) using a class literal(字面的). See Section 15.8.2 ofThe Java™ LanguageSpecification(规范). For example:

    System.out.println("The name of class Foo is: "+Foo.class.getName());

    java.lang.reflect

    Class Method

    • All Implemented Interfaces:
      AnnotatedElement, GenericDeclaration, Member


      public final class Method
      extends AccessibleObject
      implements GenericDeclaration, Member
      A Method provides(提供) information(信息) about(关于), and access( 访问) to, a single method on a class or interface. The reflected(反射) method may be a class method or an instance method (including an abstract method).

      A Method permits(允许) widening(扩展) conversions tooccur(发生) whenmatching(匹配) the actual(实际的) parameters to invoke with theunderlying(基础) method'sformal(形式的) parameters, but it throws anIllegalArgumentException if anarrowing(收缩) conversion would occur.


由于给定的参考引用未涉及 'Illegal reference to parent class method' 错误相关内容,下面从通用的 Java 编程知识来分析解决此错误的方法。 ### 错误原因分析 通常,'Illegal reference to parent class method' 错误往往是在引用父类方法时,使用方式不符合 Java 语法规则。可能的情况有: - 方法未在父类中定义。 - 方法访问权限问题,比如尝试访问父类的私有方法。 - 方法调用时参数不匹配。 ### 解决方法 #### 1. 检查方法是否在父类中定义 确保要调用的方法确实在父类中存在。例如: ```java class Parent { public void parentMethod() { System.out.println("This is a parent method."); } } class Child extends Parent { public void callParentMethod() { // 正确调用父类方法 super.parentMethod(); } } ``` #### 2. 检查方法访问权限 保证方法的访问权限允许在子类中调用。父类的私有方法不能在子类中直接调用,受保护的和公共方法可以。 ```java class Parent { private void privateMethod() { System.out.println("This is a private method."); } protected void protectedMethod() { System.out.println("This is a protected method."); } } class Child extends Parent { public void callParentMethod() { // 错误调用,不能访问父类私有方法 // super.privateMethod(); // 正确调用,父类受保护方法可以在子类中访问 super.protectedMethod(); } } ``` #### 3. 检查方法参数匹配 调用父类方法时,传入的参数类型和数量要与父类方法定义一致。 ```java class Parent { public void parentMethod(int num) { System.out.println("The number is: " + num); } } class Child extends Parent { public void callParentMethod() { // 正确调用,传入正确的参数类型 super.parentMethod(10); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值