Compare the Difference Between Similar Terms

之间的区别

Home / 技术 / 它 / 编程 /类和实例变量之间的区别

类和实例变量之间的区别

2018年2月5日发表Lithmee

Key Difference – Classvs Instance Variables

大多数现代编程语言支持面向对象的编程。一个目的contains data or attributes. An object has certain behaviors. They are known as methods. A program can be model using objects. A software is a collection of programs. Therefore, a software can be designed and implemented using objects. Objects are interacted using methods. Object-Oriented Programming improves code modularity and reusability. There should be a class to create objects. A class is a blueprint to create an object. Therefore, an object is an instance of a class. In programming, the data needed to be stored. Data is stored in memory locations. These memory locations are called variables. A member variable is a variable that is associated with a specific object. It is accessible for all its methods. There are two types of member variables that are class variables and instance variables. The关键区别between class and instance variables is that,如果只有与类的所有实例共享的变量的副本,则这些变量称为类变量,如果类的每个实例都有其自己的变量副本,则这些变量称为实例变量。

CONTENTS

1。概述和关键差异
2。什么是类变量
3。What are Instance Variables
4。类和实例变量之间的相似之处
5。并排比较 - 表格形式的类VS实例变量
6.Summary

什么是类变量?

当只有一个与类的所有实例共享的变量的副本时,这些变量被称为类变量。类变量是在任何方法之外的类中声明的变量。这些变量包含关键字静态。这些变量与类关联,而不是与对象关联。

类和实例变量之间的区别

Figure 01: Class Variables and Instance Variables

参考具有类变量的代码以下代码。

公共班级员工{

public static int id;

public static double salary;

}

公共类测试{

公共静态void main(string [] args){

员工e1 = new Employee();

Employee e2= new Employee();

}

}

根据上述程序,E1和E2是员工类型对象。两者都将具有相同的内存副本。如果e1.id = 1和打印e2.ID也将给出值1.可以使用员工班级名称(例如员工,雇员,salary等)打印ID和工资值。

什么是实例变量?

当类的每个实例都有自己的变量副本时,这些变量被称为实例变量。请参阅以下程序。

公共班级员工{

public int id;

公共双工资;

}

公共类测试{

公共静态void main(string [] args){

员工e1 = new Employee();

e1.id = 1;

e1.salary= 20000;

Employee e2= new Employee();

e2.id = 2;

E2。工资= 25000;

}

}

在主要程序中,E1和E2是对类型员工的对象的引用。可以使用点运算符(例如e1.id,e1)分配ID和工资的值。工资等。班级员工中的ID和薪水被称为实例变量。E1和E2是单独的对象。每个对象将具有实例变量的单独副本。E1将具有单独的ID,薪水和E2将具有单独的ID和工资。因此,创建对象或实例时创建实例变量。

What are the Similarities Between Class and Instance Variables?

  • 两者都是变量的类型。
  • Both variables are inside a class but outside any method.

What is the Difference Between Class and Instance Variables?

类变量vs Instance Variables

类变量是变量,其中只有一个与类的所有实例共享的变量的副本。 当类的每个实例都有自己的变量副本时,实例变量是变量。
协会
Class variables are associated with the class. 实例变量与对象关联。
复印数量
类变量为所有对象创建一个副本。 实例变量为每个对象创建单独的副本。
Keywords
类变量应具有静态关键字。 实例变量不需要特殊关键字,例如静态。

Summary – Classvs Instance Variables

面向对象的编程是主要的编程范式。它有助于使用对象建模软件。使用类创建对象。物体创建也称为实例化。一个类提供了创建对象的蓝图。成员变量是与特定对象关联的变量。它的所有方法都可以访问。有两种类型的成员变量为类变量和实例变量。类和实例变量之间的区别在于,如果与类的所有实例共享一个变量的副本,则这些变量称为类变量,如果类的每个实例都有自己的变量的副本,则变量称为实例变量。

Download the PDF Version of Class vs Instance Variables

您可以下载本文的PDF版本,并根据引文注释将其用于离线目的。请在此处下载PDF版本:类和实例变量之间的区别

参考:

1。tutorialspoint.com. “Java Object and Classes.”重点。在这里可用
2。“Instance variable.” Wikipedia, Wikimedia Foundation, 16 Dec. 2017.在这里可用
3.“类变量。”Wikipedia,Wikimedia基金会,2017年12月16日。在这里可用

Related posts:

敏捷和Scrum之间的差异 之间的区别Java and Oracle 之间的区别DLL and LIB 开源和专有软件之间的区别开源和专有软件之间的区别 平行计算和分布式计算之间的差异平行计算和分布式计算之间的差异

提交以下:编程Tagged With:类和实例变量差异,,,,类和实例变量的相似性,,,,类变量,,,,类变量关联,,,,类变量副本,,,,类变量定义,,,,类变量Keywords,,,,Class vs Instance Variables,,,,比较类和实例变量,,,,实例变量,,,,实例变量协会,,,,实例变量副本,,,,实例变量定义,,,,实例变量关键字

关于作者:Lithmee

LithmeeMandula is a BEng (Hons) graduate in Computer Systems Engineering. She is currently pursuing a Master’s Degree in Computer Science. Her areas of interests in writing and research include programming, data science, and computer systems.

Leave a ReplyCancel reply

您的电子邮件地址不会被公开。必需的地方已做标记*

Request Article

精选文章

冠状病毒和冷症状之间的差异

冠状病毒和冷症状之间的差异

之间的区别Coronavirus and SARS

之间的区别Coronavirus and SARS

冠状病毒和流感的差异

冠状病毒和流感的差异

冠状病毒和covid 19之间的差异

冠状病毒和covid 19之间的差异

You May Like

之间的区别一个droid 6.0 Marshmallow and Android 7.0 Nougat

之间的区别一个droid 6.0 Marshmallow and Android 7.0 Nougat

之间的区别Smooth Muscle and Skeletal Muscle

之间的区别Cohort and Panel Study

之间的区别Cohort and Panel Study

SaaS和SOA之间的差异

设施和便利设施之间的差异

设施和便利设施之间的差异

Latest Posts

  • 甲基对羟基苯甲酸酯和丙泊替户之间有什么区别
  • 卡比马唑和遇到的区别是什么himazole
  • What is the Difference Between Crista and Macula
  • 相思胶和tragacanth牙龈有什么区别
  • 岩藻糖和鼠李糖有什么区别
  • What is the Difference Between Emollient and Occlusive
  • Home
  • 空缺
  • About
  • Request Article
  • Contact Us

版权所有©2010-2018之间的区别。版权所有。使用条款and Privacy Policy:Legal。