Skip to content

Instantly share code, notes, and snippets.

@miya2000
Created August 5, 2015 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miya2000/d3ed6ba9e998c0fa23f9 to your computer and use it in GitHub Desktop.
Save miya2000/d3ed6ba9e998c0fa23f9 to your computer and use it in GitHub Desktop.
protected access modifier difference between Java and C#.
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.
}
}
}
}
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