孕妇喝什么汤最好最有营养| b-h是什么药| 风花雪月是什么意思| 胆囊切除有什么危害| 肝回声密集是什么意思| 中年男人遗精是什么原因| 血精是什么原因| 胸围85是什么罩杯| 7月17是什么星座| 少将相当于什么级别| 黄连膏有什么功效和作用| 怀孕呕吐吃什么可以缓解| g750是什么金| 欲购从速什么意思| 胃肠彩超能检查出什么| 灵芝搭配什么煲汤最好| 十二生肖里为什么没有猫| 什么水果降火| 泡沫尿吃什么药| 女人下嘴唇厚代表什么| 1980年五行属什么| 为什么小孩子有白头发| 月经期不能吃什么水果| 夏天适合用什么护肤品| sub是什么意思| 肛门疼痛吃什么药| 什么东西补肾最好| 脑梗是由什么引起的| 肝火旺盛是什么原因引起的| 16岁属什么| 查脂肪肝挂什么科室| 腿肿是什么原因| 左肺钙化灶是什么意思| 二月九号是什么星座| 脸部神经跳动吃什么药| 贫血看什么指标| 蓝矾对人有什么危害| 青蒿素是什么| 早上起床眼睛浮肿是什么原因| 水手是干什么的| 咖啡豆是什么动物粪便| 汗疱疹是什么| 1995年是什么年| 打卤面都有什么卤| 冬枣什么时候上市| 86年属什么生肖| 胎盘可以治什么病| 青蛇是什么蛇| 一路卷风迎大年是什么生肖| 坦诚相待是什么意思| 心脏舒张功能减低是什么意思| 胆汁淤积吃什么药| 血常规能检查出什么| mchc偏低是什么意思| pettm和pe有什么区别| 交感神经型颈椎病吃什么药| 健康证需要什么材料| moda是什么牌子| 下肢血管堵塞吃什么药| av是什么| 榴莲是什么味道| 3月3日是什么节| 老公梦见蛇是什么预兆| lover是什么意思| 疑问是什么意思| 狗狗能吃什么水果| 雌激素分泌过多是什么原因引起的| 艳羡是什么意思| 血精和精囊炎吃什么药| 梅毒用什么药| 黑匣子是什么颜色| 奢侈品是什么意思| 牡丹什么时候开| 耍无赖是什么意思| 靠北是什么意思| 为什么会得带状疱疹| 微量泵是干什么用的| 不是月经期出血是什么原因| 白细胞低有什么危害| 跟班是什么意思| leep术是什么手术| 胃酸多吃什么药| 浑浊是什么意思| 痿是什么意思| 刀纸是什么| mc什么意思| 柔顺和拉直有什么区别| 博美犬吃什么狗粮最好| 清洁度2度是什么意思| 夯实是什么意思| 农历3月是什么星座| 什么叫尿潜血| 和谐的什么| 伐木累是什么意思| 为什么直系亲属不能输血| 一什么桃花| 白萝卜煮水喝有什么功效和作用| 胰腺癌有什么症状| 什么是紫外线| 梦见金项链是什么意思| 冰释前嫌是什么意思| tv是什么意思| 九月十五是什么星座的| 老公什么意思| 4月25号是什么星座| 什么叫透析| 狡兔三窟是什么意思| 偷鸡不成蚀把米是什么生肖| 眼睛疼吃什么药效果最好| 什么硬币最值钱| 甲状腺肿是什么意思| nibp是什么意思| 斛什么意思| 尿频是什么原因| 沪深300是什么意思| 风湿病是什么引起的| 血红蛋白低吃什么| 高校是什么意思| 毛主席的女儿为什么姓李| 男人左眼跳是什么意思| 什么繁什么茂| 什么是干眼症| 鱼泡是鱼的什么器官| 上半身胖属于什么体质| 什么眼霜比较好用| 1945年属什么| 请结合临床是什么意思| 中老年吃什么钙片比较好| 为什么要延迟退休| 什么是电子邮件地址| 家庭主妇是什么意思| 感冒适合吃什么饭菜| 丁香花长什么样| 情商是什么意思| 男人吃什么补肾壮阳效果最好| 做梦梦见棺材和死人是什么意思| 什么的叹气| 聚餐吃什么| 吃鸭蛋有什么好处和坏处| 腰椎间盘突出不能吃什么食物| 看血管挂什么科| 灵魂伴侣是什么意思| 血小板太高会导致什么| 什么药治肝最好最安全| 胎盘低是什么意思| 白蛋白偏高是什么原因| 办独生子女证需要什么材料| 1999年是什么生肖| 成都什么地方好玩| 88年属什么的| 肠胃炎吃什么消炎药| 恐惧是什么意思| 3月11日什么星座| 什么是口播| 远水解不了近渴什么意思| 猫肉什么味道| 71年猪是什么命| 偶见是什么意思| 三级医院什么意思| 低密度脂蛋白偏高吃什么好| 头皮屑挂什么科| 老年人喝什么蛋白粉好| 钼靶检查是什么| 职场是什么意思| 男人吃鸽子有什么好处| 尿液发臭是什么原因| 菲林是什么| 黄飞鸿是什么生肖| 石膏是什么| 什么的迎春花| 帕金森病是什么原因引起的| 三点水及念什么| 一什么尺子| 两肺散在小结节是什么意思| ssr是什么| 戴玉手镯有什么好处| 乳酸杆菌是什么| 名号是什么意思| 孕妇梦见坟墓是什么预兆| 动物为什么要冬眠| 颠三倒四是什么意思| 肝火胃火旺吃什么药| 股骨头在什么位置| 吃什么通便| 头伏二伏三伏吃什么| 披什么散什么| 骨折后吃什么好的快| 什么的石头| 干呕是什么原因| 肺部有问题一般会出现什么症状| 咳嗽呕吐是什么原因| 肝功能2项是指什么| 2005年属鸡是什么命| mlb是什么意思| 什么也不懂| 小孩吃鹅蛋有什么好处| 脂膜炎是什么病| 牛肉可以炒什么菜| 龙马精神代表什么生肖| 心口疼挂什么科| 口腔炎吃什么药| 西瓜为什么叫西瓜| 胸腰椎退行性变是什么意思| 表水是什么意思| 额头老出汗是什么原因| 手指甲出现双层是什么原因| 血红蛋白是什么意思| 为什么有眼袋是什么原因引起的| 海清是什么意思| 高危妊娠是什么意思啊| 孕晚期羊水多了对宝宝有什么影响| 眼睛疲劳用什么眼药水好| 人中长代表什么| 鼻炎吃什么药见效快| 哔哩哔哩是干什么的| 什么是熊猫血| 总感觉饿是什么原因| 三高人群适合吃什么水果| 男人精子少吃什么药| 槟榔长什么样子| 喜形于色是什么意思| 为什么会得带状疱疹| 沙蒜是什么| td代表什么意思| 甲胎蛋白是检查什么的| 咳嗽吃什么食物好得快| 婴儿打嗝是什么原因引起的| lf是什么牌子| 兰花代表什么象征意义| 右上眼皮跳是什么预兆| 朱元璋原名叫什么| 腱子肉是什么意思| 失眠睡不着是什么病| 什么是碱性磷酸酶高怎么回事| 梦见媳妇出轨什么预兆| 吃什么最容易减肥| 什么玉便宜又养人| 冬至有什么忌讳| 2009年是什么年| 怕金病帕金森是什么症状| 后背麻木是什么原因| 毛骨鱼是什么鱼| 梦见自己刷牙是什么意思| 如日中天是什么生肖| 地板油是什么意思| 南京立冬吃什么| 刺梨有什么功效| 暖宫贴贴在什么位置| 61年属什么| 吃东西想吐是什么原因| 射手什么象星座| 野鸡吃什么| 火花是什么| 60岁是什么之年| 泡汤是什么意思| 胸部挂什么科| 肾结石什么不能吃| 营养包是什么| 检查怀孕挂什么科| 一月27日是什么星座| 嘉庆叫什么名字| 女m是什么意思| 湿疹要注意什么| 吃什么止腹泻| 百度Jump to content

• 新能源汽车:产能已过剩 谁将为此买单?

From Wikipedia, the free encyclopedia
(Redirected from Static method)
百度 这款耳机采用了半开放式设计,能够使声场更加开阔一些,获得出色的音效。

A method in object-oriented programming (OOP) is a procedure associated with an object, and generally also a message. An object consists of state data and behavior; these compose an interface, which specifies how the object may be used. A method is a behavior of an object parametrized by a user.

Data is represented as properties of the object, and behaviors are represented as methods. For example, a Window object could have methods such as open and close, while its state (whether it is open or closed at any given point in time) would be a property.

In class-based programming, methods are defined within a class, and objects are instances of a given class. One of the most important capabilities that a method provides is method overriding - the same name (e.g., area) can be used for multiple different kinds of classes. This allows the sending objects to invoke behaviors and to delegate the implementation of those behaviors to the receiving object. A method in Java programming sets the behavior of a class object. For example, an object can send an area message to another object and the appropriate formula is invoked whether the receiving object is a rectangle, circle, triangle, etc.

Methods also provide the interface that other classes use to access and modify the properties of an object; this is known as encapsulation. Encapsulation and overriding are the two primary distinguishing features between methods and procedure calls.[1]

Overriding and overloading

[edit]

Method overriding and overloading are two of the most significant ways that a method differs from a conventional procedure or function call. Overriding refers to a subclass redefining the implementation of a method of its superclass. For example, findArea may be a method defined on a shape class,[2] triangle, etc. would each define the appropriate formula to calculate their area. The idea is to look at objects as "black boxes" so that changes to the internals of the object can be made with minimal impact on the other objects that use it. This is known as encapsulation and is meant to make code easier to maintain and re-use.

Method overloading, on the other hand, refers to differentiating the code used to handle a message based on the parameters of the method. If one views the receiving object as the first parameter in any method then overriding is just a special case of overloading where the selection is based only on the first argument. The following simple Java example illustrates the difference:

Accessor, mutator and manager methods

[edit]

Accessor methods are used to read the data values of an object. Mutator methods are used to modify the data of an object. Manager methods are used to initialize and destroy objects of a class, e.g. constructors and destructors.

These methods provide an abstraction layer that facilitates encapsulation and modularity. For example, if a bank-account class provides a getBalance() accessor method to retrieve the current balance (rather than directly accessing the balance data fields), then later revisions of the same code can implement a more complex mechanism for balance retrieval (e.g., a database fetch), without the dependent code needing to be changed. The concepts of encapsulation and modularity are not unique to object-oriented programming. Indeed, in many ways the object-oriented approach is simply the logical extension of previous paradigms such as abstract data types and structured programming.[3]

Constructors

[edit]

A constructor is a method that is called at the beginning of an object's lifetime to create and initialize the object, a process called construction (or instantiation). Initialization may include an acquisition of resources. Constructors may have parameters but usually do not return values in most languages. See the following example in Java:

public class Main {
    String _name;
    int _roll;

    Main(String name, int roll) { // constructor method
        this._name = name;
        this._roll = roll;
    }
}

Destructor

[edit]

A Destructor is a method that is called automatically at the end of an object's lifetime, a process called Destruction. Destruction in most languages does not allow destructor method arguments nor return values. Destructors can be implemented so as to perform cleanup chores and other tasks at object destruction.

Finalizers

[edit]

In garbage-collected languages, such as Java,[4]:?26,?29? C#,[5]:?208–209? and Python, destructors are known as finalizers. They have a similar purpose and function to destructors, but because of the differences between languages that utilize garbage-collection and languages with manual memory management, the sequence in which they are called is different.

Abstract methods

[edit]

An abstract method is one with only a signature and no implementation body. It is often used to specify that a subclass must provide an implementation of the method, as in an abstract class. Abstract methods are used to specify interfaces in some programming languages.[6]

Example

[edit]

The following Java code shows an abstract class that needs to be extended:

abstract class Shape {
    abstract int area(int h, int w); // abstract method signature
}

The following subclass extends the main class:

public class Rectangle extends Shape {
    @Override
    int area(int h, int w) {
        return h * w;
    }
}

Reabstraction

[edit]

If a subclass provides an implementation for an abstract method, another subclass can make it abstract again. This is called reabstraction.

In practice, this is rarely used.

Example

[edit]

In C#, a virtual method can be overridden with an abstract method. (This also applies to Java, where all non-private methods are virtual.)

class IA
{
    public virtual void M() { }
}
abstract class IB : IA
{
    public override abstract void M(); // allowed
}

Interfaces' default methods can also be reabstracted, requiring subclasses to implement them. (This also applies to Java.)

interface IA
{
    void M() { }
}
interface IB : IA
{
    abstract void IA.M();
}
class C : IB { } // error: class 'C' does not implement 'IA.M'.

Class methods

[edit]

Class methods are methods that are called on a class rather than an instance. They are typically used as part of an object meta-model. I.e, for each class, defined an instance of the class object in the meta-model is created. Meta-model protocols allow classes to be created and deleted. In this sense, they provide the same functionality as constructors and destructors described above. But in some languages such as the Common Lisp Object System (CLOS) the meta-model allows the developer to dynamically alter the object model at run time: e.g., to create new classes, redefine the class hierarchy, modify properties, etc.

Special methods

[edit]

Special methods are very language-specific and a language may support none, some, or all of the special methods defined here. A language's compiler may automatically generate default special methods or a programmer may be allowed to optionally define special methods. Most special methods cannot be directly called, but rather the compiler generates code to call them at appropriate times.

Static methods

[edit]

Static methods are meant to be relevant to all the instances of a class rather than to any specific instance. They are similar to static variables in that sense. An example would be a static method to sum the values of all the variables of every instance of a class. For example, if there were a Product class it might have a static method to compute the average price of all products.

A static method can be invoked even if no instances of the class exist yet. Static methods are called "static" because they are resolved at compile time based on the class they are called on and not dynamically as in the case with instance methods, which are resolved polymorphically based on the runtime type of the object.

Examples

[edit]
In Java
[edit]

In Java, a commonly used static method is:

Math.max(double a, double b)

This static method has no owning object and does not run on an instance. It receives all information from its arguments.[2]

Copy-assignment operators

[edit]

Copy-assignment operators define actions to be performed by the compiler when a class object is assigned to a class object of the same type.

Operator methods

[edit]

Operator methods define or redefine operator symbols and define the operations to be performed with the symbol and the associated method parameters. C++ example:

#include <string>

class Data {
 public:
  bool operator<(const Data& data) const { return roll_ < data.roll_; }
  bool operator==(const Data& data) const {
    return name_ == data.name_ && roll_ == data.roll_;
  }

 private:
  std::string name_;
  int roll_;
};

Member functions in C++

[edit]

Some procedural languages were extended with object-oriented capabilities to leverage the large skill sets and legacy code for those languages but still provide the benefits of object-oriented development. Perhaps the most well-known example is C++, an object-oriented extension of the C programming language. Due to the design requirements to add the object-oriented paradigm on to an existing procedural language, message passing in C++ has some unique capabilities and terminologies. For example, in C++ a method is known as a member function. C++ also has the concept of virtual functions which are member functions that can be overridden in derived classes and allow for dynamic dispatch.

Virtual functions

[edit]

Virtual functions are the means by which a C++ class can achieve polymorphic behavior. Non-virtual member functions, or regular methods, are those that do not participate in polymorphism.

C++ Example:

#include <iostream>
#include <memory>

class Super {
 public:
  virtual ~Super() = default;

  virtual void IAm() { std::cout << "I'm the super class!\n"; }
};

class Sub : public Super {
 public:
  void IAm() override { std::cout << "I'm the subclass!\n"; }
};

int main() {
  std::unique_ptr<Super> inst1 = std::make_unique<Super>();
  std::unique_ptr<Super> inst2 = std::make_unique<Sub>();

  inst1->IAm();  // Calls |Super::IAm|.
  inst2->IAm();  // Calls |Sub::IAm|.
}

See also

[edit]

Notes

[edit]
  1. ^ "What is an Object?". oracle.com. Oracle Corporation. Retrieved 13 December 2013.
  2. ^ a b Martin, Robert C. (2009). Clean Code: A Handbook of Agile Software Craftsmanship. Prentice Hall. p. 296. ISBN 978-0-13-235088-4.
  3. ^ Meyer, Bertrand (1988). Object-Oriented Software Construction. Cambridge: Prentice Hall International Series in Computer Science. pp. 52–54. ISBN 0-13-629049-3.
  4. ^ Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.
  5. ^ Albahari, Joseph. C# 10 in a Nutshell. O'Reilly. ISBN 978-1-098-12195-2.
  6. ^ "Abstract Methods and Classes". oracle.com. Oracle Java Documentation. Retrieved 11 December 2014.

References

[edit]
他们吃什么 手脚经常发麻是什么原因 挂红是什么意思 碳酸氢钠是什么 道心是什么意思
斯德哥尔摩综合征是什么 夜幕降临是什么意思 女性分泌物发黄是什么原因 舌苔发黄是什么症状 嘴唇上长痘是什么原因
脑梗吃什么药效果最好 测骨龄去医院挂什么科 非那雄胺片是什么药 红霉素软膏和眼膏有什么区别 2040年是什么年
什么是禽流感 为什么会得口腔溃疡 蛔虫和蛲虫有什么区别 un读什么 意味深长是什么意思
折叠胆囊是什么意思hcv8jop7ns3r.cn 吃什么补钾快hcv8jop2ns6r.cn 为什么说冬吃萝卜夏吃姜hcv9jop1ns9r.cn 尿酸高能吃什么水果mmeoe.com 中性粒细胞比率偏低是什么意思hcv8jop5ns4r.cn
孩子晚上睡觉磨牙是什么原因hcv7jop4ns6r.cn 双相情感障碍是什么病hcv7jop9ns1r.cn 假牙什么材质的最好hcv7jop9ns7r.cn 吃桃子有什么好处hcv9jop5ns0r.cn 柠檬什么时候开花结果hcv9jop7ns9r.cn
废电池乱丢对人体可能造成什么中毒hcv8jop7ns6r.cn 排卵期同房后要注意什么hcv8jop5ns7r.cn 口干是什么原因呢hcv8jop8ns0r.cn 息肉样增生是什么意思hcv8jop9ns6r.cn 发烧有什么症状wuhaiwuya.com
傍家儿是什么意思hcv8jop9ns7r.cn 什么品种的西瓜最好吃hcv9jop1ns8r.cn 甘油三酯低有什么危害jiuxinfghf.com 酱油和生抽有什么区别hcv7jop9ns1r.cn 点石成金是什么意思onlinewuye.com
百度