| |

VerySource

 Forgot password?
 Register
Search
View: 1869|Reply: 19

Little problems when little girls begin to learn ~~~ !!!

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-26 12:00:02
| Show all posts |Read mode
public interface Iaa
    {
        void show ();
    }
    class ba // base class
    {
        public void show ()
        {
            Console.WriteLine ("ba.show");
        }
    }

    class de: ba, Iaa // Subclass
    {
        new public void show ()
        {
            Console.WriteLine ("de.show");
        }
    }
    class Program
    {
        static void Main (string [] args)
        {
            b = new de ();
            b.show (); // why show ba.show
            ((Iaa) b) .show (); // why show de.show
            Console.Read ();
        }
    }
Reply

Use magic Report

0

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-2-16 20:45:01
| Show all posts
b = new de (); this b will have two actual shows
b.show (); // why it is displayed
((Iaa) b) .show (); // Why show de.show de is the implementation of iaa, but ba is not, so the Iaa type variable calls de show
Reply

Use magic Report

0

Threads

27

Posts

21.00

Credits

Newbie

Rank: 1

Credits
21.00

 China

Post time: 2020-2-17 20:15:01
| Show all posts
What type is b, you haven't defined it here. Defined as ba b and de b are different.
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-2-18 09:15:01
| Show all posts
public interface Iaa
    {
        void show ();
    }
    class ba // base class
    {
        public void show ()
        {
            Console.WriteLine ("ba.show");
        }
    }

    class de: ba, Iaa // Subclass
    {
        new public void show ()
        {
            Console.WriteLine ("de.show");
        }
    }
    class Program
    {
        static void Main (string [] args)
        {
            ba b = new ba ();
            b = new de ();
            b.show (); // why show ba.show
            ((Iaa) b) .show (); // why show de.show
            Console.Read ();
        }
    }
Reply

Use magic Report

0

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-2-18 10:45:01
| Show all posts
In the Main method, b = new de (); you want to specify the type of b.
If de b = new de (); is specified, calling de.show () results in de.show
If da b = new de (); is specified, the base class da.show () is called and the result is da.show
((Iaa) b) .show () only calls show () in the class (de) that implements the interface. The result is de.show
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-2-18 12:15:01
| Show all posts
What is the difference between new and override?
Reply

Use magic Report

0

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-2-19 15:30:01
| Show all posts
ba.show is not modified with virtual. In the subclass de, you can only use new, not override, and the effect of running without new is the same, but there will be a warning when compiling.
If the override method is overridden in a subclass, no matter which parent class you define, the call is the implementation that was last overridden.
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-2-19 18:45:01
| Show all posts
ba b = new ba ();
            b = new de ();
            b.show (); // why show ba.show
Don't understand why ba.show is displayed


Hope master ~~~~~~~~~~~~~~~~ `
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 Unknown

Post time: 2020-2-22 18:45:01
| Show all posts
public interface Iaa
    {
        void show ();
    }
    class ba // base class
    {
        public void show ()
        {
            Console.WriteLine ("ba.show");
        }
    }

    class de: ba, Iaa // Subclass
    {
        new public void show ()
        {
            Console.WriteLine ("de.show");
        }
    }
    class Program
    {
        static void Main (string [] args)
        {
            b = new de ();
            b.show (); // why show ba.show
            ((Iaa) b) .show (); // why show de.show
            Console.Read ();
        }
    }


class de: ba, Iaa // Subclass
It was originally written that it could not be compiled and passed, it should be that the Show () method of the interface Iaa was not implemented in de
But here happens because de also inherits ba, and there is exactly the function of Show () in ba
The name is the same as the method that needs to implement the interface

(A lot of coincidences -_- "But is there a necessity behind the coincidence)

In fact, this operation is done here so that Show () of the base class ba serves as the implementation of Show () of the interface Iaa
In other words, when we use the reference of the base class or the reference of the interface, the function pointed to is the ba.Show () function.
therefore:
((Iaa) b) .show (); // Why de.show is displayed -------- So ba.show is displayed

But here again because of the use of the new keyword, all when we use the reference of de to access the Show () function
The function no longer points to the ba method in the base class, but to its own new method Show ()
therefore:
b.show (); // why show ba.show ----------- so show de.show

Too many coincidences!


------------------------------------- I don't know if it's right, but the analysis is not executed ---_ -"-
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 United States

Post time: 2020-2-23 16:30:01
| Show all posts
FT!
The result is
de.show
de.show

I understand wrong
======================================================= ===========================
class de: ba, Iaa // Subclass
It was originally written that it could not be compiled and passed, it should be that the Show () method of the interface Iaa was not implemented in de
But here happens because de also inherits ba, and there is exactly the function of Show () in ba
The name is the same as the method that needs to implement the interface
======================================================= ============================
This will compile without error
class de: Iaa // Subclass
    {
        new public void show ()
        {
            Console.WriteLine ("de.show");
        }
    }



======================================================= ===========================
In fact, this operation is done here so that Show () of the base class ba serves as the implementation of Show () of the interface Iaa
In other words, when we use the reference of the base class or the reference of the interface, the function pointed to is the ba.Show () function.
therefore:
((Iaa) b) .show (); // Why de.show is displayed -------- So ba.show is displayed
======================================================= ===========================

Finally, the new public Show () in de is implemented as an interface
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list