Created
August 5, 2015 10:48
-
-
Save miya2000/d3ed6ba9e998c0fa23f9 to your computer and use it in GitHub Desktop.
protected access modifier difference between Java and C#.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
namespace UnitTestProject1 | |
{ | |
[TestClass] | |
public class UnitTest1 | |
{ | |
[TestMethod] | |
public void TestMethod1() | |
{ | |
new B(new A()).Hoge().Is(10); | |
} | |
class A | |
{ | |
protected int X = 10; | |
} | |
class B : A | |
{ | |
private A _A; | |
public B(A a) | |
{ | |
_A = a; | |
} | |
public int Hoge() | |
{ | |
//return _A.X; // Compile Error: CS1540 Cannot access protected member 'UnitTest1.A.X' via a qualifier of type 'UnitTest1.A'; the qualifier must be of type 'UnitTest1.B' (or derived from it) | |
return base.X; // OK. | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package UnitTestProject1; | |
import static org.junit.Assert.*; | |
import static org.hamcrest.CoreMatchers.*; | |
import org.junit.Test; | |
public class UnitTest1 { | |
@Test | |
public void test() { | |
assertThat(new B(new A()).Hoge(), is(10)); | |
} | |
class A { | |
protected int X = 10; | |
} | |
class B extends A { | |
private A _A; | |
public B(A a) { | |
_A = a; | |
} | |
public int Hoge() { | |
return _A.X; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment