プログラム言語 スコープ

private/public

//global変数
var i = "global"

function myFunction()
{
  //local変数
  var i = "local"
}

console.log(i);
// ⇒ i : "global"

var宣言を行わない事による弊害に注意
//global変数
var i = "global"

function myFunction()
{
  //varを付けない場合
  //宣言でなく既存変数を上書き = global変数を上書き
  i = "local"
}

console.log(i);
// ⇒ i : "local"

JavaScriptにブロックスコープはない
if(true)
{
  var data = 10;
}
console.log(data);
⇒ 10 (if文内の変数にアクセスできる)

protected

private
同一クラス内からはアクセス可
インスタンスからはアクセス不可
派生クラスから : クラス内からでもアクセス不可
派生クラスから : インスタンスからはアクセス不可
protected
同一クラス内からはアクセス可
インスタンスからはアクセス不可
派生クラスから : クラス内からはアクセス
派生クラスから : インスタンスからはアクセス不可

public class Parent
{
  public int memberPublic { get; set; }
  private int memberPrivate { get; set; }
  protected int memberProtected { get; set; }
}

public class Child : Parent
{
  public void action()
  {
    base.memberPublic = 10; ⇒Public : OK
    base.memberPrivate = 10; ⇒Private : NG
    //privateメンバーは派生クラスからアクセス不可
    base.memberProtected = 10; ⇒Protected :OK
    //protectedメンバーは派生クラスからはアクセス
  }
}

public void myForm_Load(object sender, EventArgs e)
{
  Parent myParent = new Parent();
  myParent.memberPublic = 50; ⇒Public : OK

  myParent.memberPrivate = 50; ⇒Private : NG
  myParent.memberProtected = 50; ⇒Protected :NG
  //Private&Protectedは外部からアクセス不可

  Child myChild = new Child();
  myChild.memberPublic = 50; ⇒Public : OK

  myChild.memberPrivate = 50; ⇒Private : NG
  myChild.memberProtected = 50; ⇒Protected :NG
  //memberPrivate&3は外部からアクセス不可
}

Public Class Parent
  Private _memberPublic As Integer
  Private _memberPrivate As Integer
  Private _memberProtected As Integer

  Public Property memberPublic As Integer
    略
  End Property

  Private Property memberPrivate As Integer
    略
  End Property

  Protected Property memberProtected As Integer
    略
  End Property
End Class

Public Class Child
  Inherits Parent

  Public Sub Action()
    MyBase.memberPublic = 50 ⇒Public : OK
    MyBase.memberPrivate = 50 ⇒Private : NG
    ‘privateメンバーは派生クラスからアクセス不可

    MyBase.memberProtected = 50 ⇒Protected :OK
    ‘protectedメンバーは派生クラスからはアクセス
  End Sub

End Class

Private Sub myForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load

  Dim myParent As New Parent()
  myParent.memberPublic = 10 ⇒Public : OKOK

  myParent.memberPrivate = 10 ⇒Private : NG
  myParent.memberProtected = 10 ⇒Protected :NG
  ‘memberPrivate&3は外部からアクセス不可

  Dim myChild As New Child()
  myChild.memberPublic = 100 ⇒Public : OKOK

  myChild.memberPrivate = 100 ⇒Private : NG
  myChild.memberProtected = 100 ⇒Protected :NG
  ‘memberPrivate&3は外部からアクセス不可
End Sub

internal/Friend

internal/Friend
同一ファイルから
クラス内 : アクセス
インスタンスから : アクセス
別ファイルから
クラス内 : アクセス不可
インスタンスから : アクセス不可

【InternalTest.dll】
public class Parent
{
  public int memberPublic { get; set; }
  private int memberPrivate { get; set; }
  protected int memberProtected { get; set; }
  internal int memberInternal { get; set; }
}

【↑とは別のexe】

using InternalTest;
//↑を参照

public class MyInternal
{
  internal int memberInternal { get; set; }
}

public void myForm_Load(object sender, EventArgs e)
{
  Parent myParent = new Parent();
  myParent.memberPublic = 50; ⇒Public : OK

  myParent.memberPrivate = 50; ⇒Private : NG
  myParent.memberProtected = 50; ⇒Protected :NG
  //private、protectedなので外部からアクセスできない

  myParent.memberInternal = 50; ⇒Internal : NG
  //internalなので外部ファイルからアクセスできない

  MyInternal myInternal = new MyInternal();
  myInternal.memberInternal = 50; ⇒Internal : OK
  //internalは同一ファイルからはアクセス可能
}

【FriendTest.dll】
Public Class Parent
  Private _memberPublic As Integer
  Private _memberPrivate As Integer
  Private _memberProtected As Integer
  Private _memberInternal As Integer

  Public Property memberPublic As Integer
    略
  End Property

  Private Property memberPrivate As Integer
    略
  End Property

  Protected Property memberProtected As Integer
    略
  End Property

  Friend Property memberInternal As Integer
    略
  End Property
End Class

【↑とは別のexe】

Imports FriendTest
↑を参照

Private Class MyFriend
  Private _memberInternal As Integer
  Friend Property memberInternal As Integer
    略
  End Property
End Class

Private Sub myForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load

  Dim myParent As New Parent()
  myParent.memberPublic = 10 ⇒Public : OK

  myParent.memberPrivate = 10 ⇒Private : NG
  myParent.memberProtected = 10 ⇒Protected :NG
  ’private、protectedは外部からアクセスできない

  myParent.memberInternal = 10 ⇒Internal : NG
  ‘Friendは別ファイルからアクセスできない

  Dim myFriend As New MyFriend()
  myFriend.memberInternal = 10 ⇒Internal : OK
  ‘Friendは同一ファイルからはアクセス可

End Sub

protected internal/Protected Friend

【InternalTest.dll】
public class OuterParent
{
  public int memberPublic { get; set; }
  private int memberPrivate { get; set; }
  protected int memberProtected { get; set; }
  internal int memberInternal { get; set; }
  protected internal int memberProtectedInternal { get; set; }
}

public class OuterChild : OuterParent
{
  public void Action()
  {
    base.memberPublic = 10; //Public : OK
    base.memberPrivate = 10; //Private : NG
    base.memberProtected = 10; //Protected : OK
    base.memberInternal = 10; //Internal : OK
    base.memberProtectedInternal = 10; //protected Internal : OK
  }
}

【↑とは別のexe】

using InternalTest;
//↑を参照

private class InnerChild : OuterParent
{
  public void Action()
  {
    base.memberPublic = 10; //Public : OK
    base.memberPrivate = 10; //Private : NG
    base.memberProtected = 10; //Protected : OK
    //派生メンバーからアクセス
    base.memberInternal = 10; //Internal : NG
    //外部ファイルからはアクセス不可
    base.memberProtectedInternal = 10; //protected Internal : OK
    //外部ファイルからだが、protectedなのでアクセス
  }
}

public void main()
{
  OuterParent myOuterParent = new OuterParent();
  myOuterParent.memberPublic = 10; //Public : OK
  myOuterParent.memberPrivate = 10; //Private : NG
  myOuterParent.memberProtected = 10; //Protected : NG
  myOuterParent.memberInternal = 10; //Internal : NG
  myOuterParent.memberProtectedInternal = 10; //protected Internal : NG

  OuterChild myOuterChild = new OuterChild();
  myOuterChild.memberPublic = 10; //public : OK
  myOuterChild.memberPrivate = 10; //Private : NG
  myOuterChild.memberProtected = 10; //Protected : NG
  myOuterChild.memberInternal = 10; //Internal : NG
  myOuterChild.memberProtectedInternal = 10; //protected Internal : NG

  InnerChild myInnerChild = new InnerChild();
  myInnerChild.memberPublic = 10; //public : OK
  myInnerChild.memberPrivate = 10; //Private : NG
  myInnerChild.memberProtected = 10; //Protected : NG
  myInnerChild.memberInternal = 10; //Internal : NG
  myInnerChild.memberProtectedInternal = 10; //protected Internal : NG
}

【InternalTest.dll】
Public Class OuterParent

  Private _memberPublic As Integer
  Private _memberPrivate As Integer
  Private _memberProtected As Integer
  Private _memberFriend As Integer
  Private _memberProtectedFriend As Integer

  Public Property memberPublic As Integer
    略
  End Property

  Private Property memberPrivate As Integer
    略
  End Property

  Protected Property memberProtected As Integer
    略
  End Property

  Friend Property memberFriend As Integer
    略
  End Property

  Protected Friend Property memberProtectedFriend As Integer
    略
  End Property
End Class

Public Class OuterChild
  Inherits OuterParent

  Public Sub Action()
    MyBase.memberPublic = 10  ’⇒Public : OK
    MyBase.memberPrivate = 10  ’⇒Private : NG
    MyBase.memberProtected = 10  ’⇒Protected : OK
    MyBase.memberFriend = 10  ’⇒Friend : OK
    MyBase.memberProtectedFriend = 10  ’⇒ProtectedFriend : OK
  End Sub

End Class

【↑とは別のexe】

Imports FriendTest
//↑を参照

Private Class InnerChild
  Inherits OuterParent

  Public Sub Action()
    MyBase.memberPublic = 10  ’⇒Public : OK
    MyBase.memberPrivate = 10  ’⇒Private : NG
    MyBase.memberProtected = 10  ’⇒Protected : OK
    ‘派生メンバーからアクセス
    MyBase.memberFriend = 10  ’⇒Friend : NG
    ‘外部ファイルからはアクセス不可
    MyBase.memberProtectedFriend = 10  ’⇒ProtectedFriend : OK
    ‘外部ファイルからだが、protectedなのでアクセス
  End Sub
End Class

Private Sub myForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load

  Dim myOuterParent As New OuterParent()
  myOuterParent.memberPublic = 10  ’⇒Public : OK
  myOuterParent.memberPrivate = 10  ’⇒Private : NG
  myOuterParent.memberProtected = 10  ’⇒Protected : NG
  myOuterParent.memberFriend = 10  ’⇒Friend : NG
  myOuterParent.memberProtectedFriend = 10  ’⇒ProtectedFriend : NG

  Dim myOuterChild As New OuterChild()
  myOuterChild.memberPublic = 10  ’⇒Public : OK
  myOuterChild.memberPrivate = 10  ’⇒Private : NG
  myOuterChild.memberProtected = 10  ’⇒Protected : NG
  myOuterChild.memberFriend = 10  ’⇒Friend : NG
  myOuterChild.memberProtectedFriend = 10  ’⇒ProtectedFriend : NG

  Dim myInnerChild As New InnerChild()
  myInnerChild.memberPublic = 10  ’⇒Public : OK
  myInnerChild.memberPrivate = 10  ’⇒Private : NG
  myInnerChild.memberProtected = 10  ’⇒Protected : NG
  myInnerChild.memberFriend = 10  ’⇒Friend : NG
  myInnerChild.memberProtectedFriend = 10  ’⇒ProtectedFriend : NG

End Sub

ASP.NET MVC

次の記事

ASP.NET MVC DB操作