Is there a way to override class variables in Java?

后端 未结 17 1061
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 09:49
class Dad
{
    protected static String me = \"dad\";

    public void printMe()
    {
        System.out.println(me);
    }
}

class Son extends Dad
{
    protected         


        
相关标签:
17条回答
  • 2020-11-22 10:19

    This looks like a design flaw.

    Remove the static keyword and set the variable for example in the constructor. This way Son just sets the variable to a different value in his constructor.

    0 讨论(0)
  • 2020-11-22 10:20

    You can create a getter and then override that getter. It's particularly useful if the variable you are overriding is a sub-class of itself. Imagine your super class has an Object member but in your sub-class this is now more defined to be an Integer.

    class Dad
    {
            private static final String me = "dad";
    
            protected String getMe() {
                return me;
            }
    
            public void printMe()
            {
                    System.out.println(getMe());
            }
    }
    
    class Son extends Dad
    {
            private static final String me = "son";
    
            @Override
            protected String getMe() {
                return me;
            }
    }
    
    public void doIt()
    {
            new Son().printMe(); //Prints "son"
    }
    
    0 讨论(0)
  • 2020-11-22 10:20
    class Dad
    {
        protected static String me = "dad";
    
        public void printMe()
        {
            System.out.println(me);
        }
    }
    
    class Son extends Dad
    {
        protected static String _me = me = "son";
    }
    
    public void doIt()
    {
        new Son().printMe();
    }
    

    ... will print "son".

    0 讨论(0)
  • 2020-11-22 10:23

    Yes. But as the variable is concerned it is overwrite (Giving new value to variable. Giving new definition to the function is Override).Just don't declare the variable but initialize (change) in the constructor or static block.

    The value will get reflected when using in the blocks of parent class

    if the variable is static then change the value during initialization itself with static block,

    class Son extends Dad {
        static { 
           me = 'son'; 
        }
    }
    

    or else change in constructor.

    You can also change the value later in any blocks. It will get reflected in super class

    0 讨论(0)
  • 2020-11-22 10:23

    Yes, just override the printMe() method:

    class Son extends Dad {
            public static final String me = "son";
    
            @Override
            public void printMe() {
                    System.out.println(me);
            }
    }
    
    0 讨论(0)
  • 2020-11-22 10:27

    If you are going to override it I don't see a valid reason to keep this static. I would suggest the use of abstraction (see example code). :

         public interface Person {
            public abstract String getName();
           //this will be different for each person, so no need to make it concrete
            public abstract void setName(String name);
        }
    

    Now we can add the Dad:

    public class Dad implements Person {
    
        private String name;
    
        public Dad(String name) {
            setName(name);
        }
    
        @Override
        public final String getName() {
        return name;
        }
    
        @Override
        public final void setName(String name) {
            this.name = name;
        }
    }
    

    the son:

    public class Son implements Person {
    
        private String name;
    
        public Son(String name) {
            setName(name);
        }
    
        @Override
        public final String getName() {
            return name;
        }
    
        @Override
        public final void setName(String name) {
            this.name = name;
        }
    }
    

    and Dad met a nice lady:

    public class StepMom implements Person {
    
        private String name;
    
        public StepMom(String name) {
            setName(name);
        }
    
        @Override
        public final String getName() {
            return name;
        }
    
        @Override
        public final void setName(String name) {
            this.name = name;
        }
    }
    

    Looks like we have a family, lets tell the world their names:

    public class ConsoleGUI {
    
        public static void main(String[] args) {
            List<Person> family = new ArrayList<Person>();
            family.add(new Son("Tommy"));
            family.add(new StepMom("Nancy"));
            family.add(new Dad("Dad"));
            for (Person person : family) {
                //using the getName vs printName lets the caller, in this case the
                //ConsoleGUI determine versus being forced to output through the console. 
                System.out.print(person.getName() + " ");
                System.err.print(person.getName() + " ");
                JOptionPane.showMessageDialog(null, person.getName());
        }
    }
    

    }

    System.out Output : Tommy Nancy Dad
    System.err is the same as above(just has red font)
    JOption Output:
    Tommy then
    Nancy then
    Dad

    0 讨论(0)
提交回复
热议问题