开发者

Calling parent method within a parent method

开发者 https://www.devze.com 2023-03-10 16:15 出处:网络
I have 2 classes, lets call them parentClass and childClass. parentClass has two methods, lets call them firstMethods and secondMethods. childClass inherits parentClass and implements firstMethod. par

I have 2 classes, lets call them parentClass and childClass. parentClass has two methods, lets call them firstMethods and secondMethods. childClass inherits parentClass and implements firstMethod. parentClass implements both methods. And here is my problem. In my parentClass in side of secondMethods I want to call firstMethods but when开发者_运维百科 I do it with [self firstMethods] I jump into implementation of childClass. If I call it with [super firstMethods] it calls method in a super class (in this example it's UIView).

So, is it possible in objective c to call methods in side of a base class? One methods calling other method without jumping in a concrete class implementation?


You dont. Change your design.

To explain this here is a some code. (My example uses ruby because it's easy to read for almost any programmer, but this is not a ObjC question, its about classes and inheritance).

class A
  def one
    "one"
  end
  def two
    self.one + "-two"
  end
end

class B < A
  def one
    "B-"+ super
  end
end

a = A.new
puts a.one #=> "one"
puts a.two #=> "one-two"

b = B.new
puts b.one #=> "B-one"
puts b.two #=> "B-one-two"

So class B overrides the one method from it's parent with it's own implementation. Which is picked up even if we dont directly use that method. This is an awesome feature of any class based language. class B has it's own way to do a one and no matter how it's asked to do it, it does it it's own way. In fact the whole idea of of the child class overriding a method is that it wants to do something in a different or augmented way from it's parent.

To get around this, you need avoid the issue entirely. Instead refactor out the internals to another method you do not override. Then your base class and child class both can call that other method for data. The cool thing now is that your child can now override this other method if it needs to as well.

class A
  def one
    one_string
  end
  def two
    self.one_string + "-two"
  end
  def one_string
    "one"
  end
end

class B < A
  def one
    "B-"+ self.one_string
  end
end

a = A.new
puts a.one #=> "one"
puts a.two #=> "one-two"

b = B.new
puts b.one #=> "B-one"
puts b.two #=> "one-two"

In this example we added a third method that class B inherits and does not override.

The whole point here is that the child class retains control over what code gets run, NOT the parent class. This is important. Your desired solution would mean you have to change the base class if you wanted to change this behavior. But simply letting inheritance be awesome allows you the most flexibility by letting the child classes define exactly how the want to use the methods exposed by their parents.


If you have object of childClass then [self firsteMethod] will call childClass implementation [super firsMethod] will call parentImplementation.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号