为什么我可以从main方法访问私有变量? [英] Why can I access a private variable from main method?

查看:146
本文介绍了为什么我可以从main方法访问私有变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

package com.valami;

 public class Ferrari
 {
  private int v = 0;


  private void alam()
  {
   System.out.println("alam");
  }

  public Ferrari()
  {
   System.out.println(v);
  }



  public static void main(String[] args)
  {
   Ferrari f = new Ferrari();
   f.v = 5;
   System.out.println(f.v);
  }

 }

大家好!
我有一个简单的问题....为什么我可以从main方法到达私有变量?我知道,我在收容班,但它是主要的。我相信主要不是包含它的类的一部分...然后我不会找到私人成员,但我可以....为什么?
请帮助...... thx

Hi all! I have one simple question.... WHY can I reach a private variable from the main method ? I know, I'm in the containing class, but it is main. I believed the main is NOT part of the class which is containing it... Then I would not to reach an private member, but I can....WHY? Please help...thx

推荐答案

主要是你班级的一部分,你已经在课堂上宣布了它:)
主要不是你的对象的一部分,它不是你从类创建的对象的任何部分,但它仍然是类的一部分。这对于任何静态函数都是正确的,因为main只是一个正常的静态函数,框架知道它应该在程序执行时查找。

Main is a part of you class, you have declared it inside your class :) What main is not is part of your object, it will not be any part of the objects you create from the class but it is still part of the class. This is correct for any static function as main is just a normal static function that the framework knows it should look for when the program is executed.

/ Viktor

这篇关于为什么我可以从main方法访问私有变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆