使用子非抽象类将值插入抽象类中的非抽象方法 [英] Inserting values to non abstract methods in abstract class using a sub non abstract class

查看:103
本文介绍了使用子非抽象类将值插入抽象类中的非抽象方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从另一个扩展(SubAbstract.java)的类中的Abstract Class(MainAbstract.java)中访问和使用3个私有变量。



从子类我想要访问主类的getters()和setters()。



在主类中(这是一个抽象类)有一个名为ShowInfo()的抽象方法。



这个ShowInfo()抽象方法应该做一些事情来查看子类的每个实例。



以下是MainClass(Abstract)和Sub Class SubAbstract的源代码。请参考。



我的尝试:



MainAbstract.class



I want to access and use 3 private variables in an Abstract Class(MainAbstract.java) from another class that has extended (SubAbstract.java) from the previously mentioned Abstract Class.

From the sub class I want to access the getters() and setters() of the main class's.

In the main class (this is an abstract class) there is an abstract method called ShowInfo().

This ShowInfo() abstract method should do something to view the each instance of the subclass's.

Below is the source code for the MainClass(Abstract) and the Sub Class SubAbstract. Please refer them.

What I have tried:

MainAbstract.class

package abstractionexample;
public abstract class MainAbstract {

    private String sName;
    private String sType;
    private int iQty;

     public String getsName() {
        return sName;
     }

     public void setsName(String sName) {
         this.sName = sName;
      }

     public String getsType() {
        return sType;
      }

     public void setsType(String sType) {
        this.sType = sType;
     }

      public int getiQty() {
         return iQty;
     }

       public void setiQty(int iQty) {
           this.iQty = iQty;
       }

        public abstract void showInfo();

        public static void main(String[] args) {       

      }
  }





SubAbstract.java





SubAbstract.java

package abstractionexample;

public class SubAbstract extends MainAbstract{

   @Override
    public void showInfo() {

    }

     //This is an instance and the getters() and setters() should use each    instance of this kind of to get values and set values.
     SubAbstract nSubAbs = new SubAbstract();  

 }

推荐答案

参见抽象方法和类(Java™教程 - 学习Java语言 - 接口和继承) [ ^ ]


这篇关于使用子非抽象类将值插入抽象类中的非抽象方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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