GVKun编程网logo

subClass sc = new subClass()和superClass sc = new subClass有什么区别?(subclass of)

22

以上就是给各位分享subClasssc=newsubClass,其中也会对和superClasssc=newsubClass有什么区别?进行解释,同时本文还将给你拓展android–使用XMLLayo

以上就是给各位分享subClass sc = new subClass,其中也会对和superClass sc = new subClass有什么区别?进行解释,同时本文还将给你拓展android – 使用XML Layout作为View Subclass的视图?、Cannot subclass a class with objc_subclassing_restricted attribute、Caused by: Could not generate CGLIB subclass of class [class com.sun.proxy.$Proxy11]、Class#getSuperclass() 方法等相关知识,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

subClass sc = new subClass()和superClass sc = new subClass有什么区别?(subclass of)

subClass sc = new subClass()和superClass sc = new subClass有什么区别?(subclass of)

class superClass {}class subClass extends superClass{}public class test{    public static void main(){    superClass sc1 = new subClass();    subClass sc2 = new subClass();    //whats the difference between the two objects created using the above code?}}

答案1

小编典典

简单说明:使用时

SuperClass obj = new SubClass();

SuperClass可访问其中定义的公共方法。中定义的方法SubClass不是。

使用时

SubClass obj = new SubClass();

定义的公共方法SubClass也可以与SuperClass公共方法一起访问。

两种情况下创建的对象都是相同的。

例如:

public class SuperClass {  public void method1(){  }}public class SubClass extends SuperClass {  public void method2()  {  }}SubClass sub = new SubClass();sub.method1();  //Valid through inheritance from SuperClasssub.method2();  // ValidSuperClass superClass = new SubClass();superClass.method1();superClass.method2(); // Compilation Error since Reference is of SuperClass so only SuperClass methods are accessible.

android – 使用XML Layout作为View Subclass的视图?

android – 使用XML Layout作为View Subclass的视图?

我觉得好像我曾经知道如何做到这一点,但我现在正在画一个空白.我有一个从View(Card)扩展的类,我用 XML编写了一个布局.我想要做的是将View of Card设置为构造函数中的XML View,因此我可以使用Card中的方法来设置TextViews等等.有什么建议?代码如下:

Card.java:
(我有View.inflate(context,R.layout.card_layout,null);这是我想要做的一个例子,但它不起作用.我基本上希望该类成为View的接口,并按顺序要做到这一点,我需要以某种方式将XML布局分配给View.我是否使用了setContentView(View视图)这样的东西?View类中没有这样的方法,但有类似的东西吗?)

public class Card extends View {

    TextView tv;

    public Card(Context context) {
        super(context);
        View.inflate(context,null);
        tv = (TextView) findViewById(R.id.tv);
    }

    public Card(Context context,AttributeSet attrs,int defStyle) {
        super(context,attrs,defStyle);
        View.inflate(context,AttributeSet attrs) {
        super(context,attrs);
        View.inflate(context,null);
        tv = (TextView) findViewById(R.id.tv);
    }

    public void setText(String text) {
        tv.setText(text);
    }

}

card_layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="336dp"
    android:layout_height="280dp"
    android:layout_gravity="center"
    android:background="@drawable/card_bg"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/tv"
        android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:textSize="24dp"
    />

</LinearLayout>

解决方法

当前的设置无法实现您想要做的事情.视图(或其直接子类)表示单个视图,它没有子视图的概念,您正在尝试执行的操作. LayoutInflater不能与简单的View一起使用,因为简单的View类没有实际添加子项的方法(如addView()方法).

另一方面,用于生成子代的正确类是ViewGroup(或其中一个直接子类,如LinearLayout,FrameLayout等),它通过提供addView方法接受向其添加Views或其他ViewGroups.最后你的班级应该是:

public class Card extends ViewGroup {

    TextView tv;

    public Card(Context context) {
        super(context);
        View.inflate(context,this);
        tv = (TextView) findViewById(R.id.tv);
    }

    public Card(Context context,this);
        tv = (TextView) findViewById(R.id.tv);
    }

    public void setText(String text) {
        tv.setText(text);
    }

}

如果我记得你扩展ViewGroup时必须覆盖onLayout,那么相反(并且由于你的布局文件),你应该看看扩展LinearLayout并用xml布局替换带有merge标签的LinearLayout.

Cannot subclass a class with objc_subclassing_restricted attribute

Cannot subclass a class with objc_subclassing_restricted attribute

 

Unfortunately, that is the case. You cannot subclass a Swift class (even if it is a subclass of NSObject and available to the Objective-C runtime) because of deliberate limitations baked into Obj-C to block subclassing Swift classes in Obj-C code.

I believe the reason for this limitation is that Swift includes features that cannot be utilised in Obj-C and therefore subclasses would be restricted and would get undefined behavIoUr when implementing methods that cannot cross into Obj-C.

If Apple were to allow Obj-C -> Swift -> Obj-C subclassing, then it would be on a limited basis. Some methods wouldn’t do the same thing in Swift as they would in Obj-C selectors, and you Could theoretically declare conflicting methods in your subclass that would have different actions depending on whether you were addressing the class in Swift and Obj-C. Additionally, the Swift Compiler Couldn’t see beyond the Swift barrier and therefore may make optimisations that would break your Obj-C subclasses.

While I understand the frustration behind this, both in personal projects and philosophically, I think that this is unfortunately the better of the two options.

 

摘自:https://forums.swift.org/t/cannot-subclass-a-class-with-objc-subclassing-restricted-attribute/5674

结论:OC里无法继承Swift类,即使Swift是@objc open 修饰也不行。(2021.07.18 记录)

Caused by: Could not generate CGLIB subclass of class [class com.sun.proxy.$Proxy11]

Caused by: Could not generate CGLIB subclass of class [class com.sun.proxy.$Proxy11]

<!-- 配置sessionFactory -->
    <bean id="sqlSessionFactory">
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath*:com/xy/tpps/db/mapper/*.xml" />
    </bean>
    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <bean>
        <property name="basePackage" value="com.xy.tpps.db" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
    <!-- 配置事务管理器 -->
    <bean id="transactionManager">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
    </bean>
    <!-- 配置事务的传播特性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception" />
            <tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception" />
            <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <!-- 事务层  -->
    <aop:config proxy-target->
        <aop:pointcut id="allManagerMethod" expression="execution(* com.xy.tpps.db.*.*.*(..))" />
        <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice" />
    </aop:config>

错误信息:

Retrieved dependent beans for bean ''(inner bean)'': [txAdvice]
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name ''tppsTxn2011Service'': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.xy.tpps.db.dao.TblOrderInfoMapper com.xy.tpps.service.TppsTxn2011ServiceImpl.tblOrderInfoMapper; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ''tblOrderInfoMapper'': Post-processing of the FactoryBean''s object failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.sun.proxy.$Proxy11]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class class com.sun.proxy.$Proxy11
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
    at com.xy.tpps.main.TppsMain.main(TppsMain.java:23)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.xy.tpps.db.dao.TblOrderInfoMapper com.xy.tpps.service.TppsTxn2011ServiceImpl.tblOrderInfoMapper; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ''tblOrderInfoMapper'': Post-processing of the FactoryBean''s object failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.sun.proxy.$Proxy11]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class class com.sun.proxy.$Proxy11
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    ... 13 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ''tblOrderInfoMapper'': Post-processing of the FactoryBean''s object failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.sun.proxy.$Proxy11]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class class com.sun.proxy.$Proxy11
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:167)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:103)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1514)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:252)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1014)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:957)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:855)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
    ... 15 more
Caused by: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.sun.proxy.$Proxy11]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class class com.sun.proxy.$Proxy11
    at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:212)
    at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:111)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:494)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:379)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:339)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:421)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.postProcessObjectFromFactoryBean(AbstractAutowireCapableBeanFactory.java:1698)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:164)
    ... 23 more
Caused by: java.lang.IllegalArgumentException: Cannot subclass final class class com.sun.proxy.$Proxy11
    at org.springframework.cglib.proxy.Enhancer.generateClass(Enhancer.java:446)
    at org.springframework.cglib.transform.TransformingClassGenerator.generateClass(TransformingClassGenerator.java:33)
    at org.springframework.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
    at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
    at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
    at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:317)
    at org.springframework.aop.framework.ObjenesisCglibAopProxy.createProxyClassAndInstance(ObjenesisCglibAopProxy.java:57)
    at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:202)
    ... 30 more

Class#getSuperclass() 方法

Class#getSuperclass() 方法

// 接口的父类为 null
        System.out.println(Serializable.class.getSuperclass()); // null
        System.out.println(java.util.List.class.getSuperclass()); // null

        // Object 的父类为 null
        System.out.println(Object.class.getSuperclass()); // null

        // 原生数据类型的父类为 null
        System.out.println(int.class.getSuperclass()); // null

        // void 类型的父类为 null
        System.out.println(Void.TYPE.getSuperclass()); // null

        // 数组的父类是 Object
        System.out.println(int[].class.getSuperclass()); // class java.lang.Object
        System.out.println(java.util.List[].class.getSuperclass()); // class java.lang.Object

        // 类、抽象类的父类就是父类
        System.out.println(ArrayList.class.getSuperclass()); // class java.util.AbstractList
        System.out.println(AbstractList.class.getSuperclass()); // class java.util.AbstractCollection

今天关于subClass sc = new subClass和superClass sc = new subClass有什么区别?的讲解已经结束,谢谢您的阅读,如果想了解更多关于android – 使用XML Layout作为View Subclass的视图?、Cannot subclass a class with objc_subclassing_restricted attribute、Caused by: Could not generate CGLIB subclass of class [class com.sun.proxy.$Proxy11]、Class#getSuperclass() 方法的相关知识,请在本站搜索。

本文标签: