Agnostic language The main programming issue

This is a very simple question from the point of view of programming, but since I am in the learning phase, I thought it was better to ask this question and not have a misunderstanding or narrow knowledge of this topic.

So excuse me if I messed it up somehow.

Question

Let's say I class A,B,C and Dnow class Ahave a piece of code that I need to have in class B,C and D, so I am expanding class Aintoclass B, class C, and class D

Now, how can I access the function class Ain other classes, I need to create an object class Aand access the function class Aor how to continue the extension Ain other classes, than I can call the function internally using this parameter.

If possible, I would really appreciate it if anyone could explain this concept with an example code that explains how logic flows.

Note

An example in Java, PHP and .Net would be appreciated.

+3
source share
6 answers

C D, , B. B A, B A. , B, A. ( , , ), . B A, (base.Foo() ).

: #

public class A
{
     public void Foo() { } 
     public virtual void Baz() { }
}

public class B : A  // B extends A
{
      public void Bar()
      {
          this.Foo();  // Foo comes from A
      }

      public override void Baz() // a new Baz
      {
          base.Baz();  // A Baz
          this.Bar();  // more stuff
      }
}

, , , B A , A ( public) .

: #

 public class B // use A from above
 {
     private A MyA { get; set; }

     public B()
     {
         this.MyA = new A();
     }

     public void Bar()
     {
         this.MyA.Foo();  // call MyA Foo()
     }
 }
+2

( .NET), - :

base.method(argumentlist);

base #

A, A

+2

.

PHP:

parent::example();

: http://www.php.net/manual/en/keyword.parent.php

<?php
class A {
    function example() {
        echo "I am A::example() and provide basic functionality.<br />\n";
    }
}

class B extends A {
    function example() {
        echo "I am B::example() and provide additional functionality.<br />\n";
        parent::example();
    }
}

$b = new B;

// This will call B::example(), which will in turn call A::example().
$b->example();
?>
0

, , B A, . A B , B , B , , .

, B A , , . , , ; , Java :

super.methodName(arg1, ...);
0

Java:

public class Aclass
{
    public static void list_examples()
    {
        return("A + B = C");
    }
}
public class Bclass extends Aclass
{
    public static void main(String [] args)
    {
        System.out.println("Example of inheritance "+list_examples);
    }
}

, . , parent:: - , /.

0

, . "B" , "C" "D" "A" , , . , "Huffy" "BrandNames" , "Huffy" , "BrandNames" . "Huffy" "" , "BrandNames" . , ( Java) , . "B" "A" , , "C" "A" "B" , "C" .

0
source

Source: https://habr.com/ru/post/1740572/


All Articles