30天代码day4 Class vs. Instance
Class
A blueprint defining the charactaristics and behaviors of an object of that class type. Class names should be written in CamelCase, starting with a capital letter.
class MyClass{
...
}
Each class has two types of variables: class variables and instance variables; class variables point to the same (static) variable across all instances of a class, and instance variables have distinct values that vary from instance to instance.
Class Constructor
Creates an instance of a class (i.e.: calling the Dog constructor creates an instance of Dog). A class can have one or more constructors that build different versions of the same type of object. A constructor with no parameters is called a default constructor; it creates an object with default initial values specified by the programmer. A constructor that takes one or more parameters (i.e.: values in parentheses) is called a parameterized constructor. Many languages allow you to have multiple constructors, provided that each constructor takes different types of parameters; these are called overloaded constructors.
class Dog{ // class name
static String unnamed = "I need a name!"; // class variable
int weight; // instance variable
String name; // instance variable
String coatColor; // instance variable Dog(){ // default constructor
this.weight = 0;
this.name = unnamed;
this.coatColor = "none";
}
Dog(int weight, String color){ // parameterized constructor
// initialize instance variables
this.weight = weight; // assign parameter's value to instance variable
this.name = unnamed;
this.coatColor = color;
}
Dog(String dogName, String color){ // overloaded parameterized constructor
// initialize instance variables
this.weight = 0;
this.name = dogName;
this.coatColor = color;
}
}
Method
A sort of named procedure associated with a class that performs a predefined action. In the sample code below, returnType will either be a data type or if no value need be returned. Like a constructor, a method can have or more parameters.
returnType methodName(parameterOne, ..., parameterN){
...
return variableOfReturnType; // no return statement if void
}
Most classes will have methods called getters and setters that get (return) or set the values of its instance variables. Standard getter/setter syntax:
class MyClass{
dataType instanceVariable;
...
void setInstanceVariable(int value){
this.instanceVariable = value;
}
dataType getInstanceVariable(){
return instanceVariable;
}
}
Structuring code this way is a means of managing how the instance variable is accessed and/or modified.
Parameter
A parenthetical variable in a function or constructor declaration (e.g.: in int methodOne(int x)
, the parameter is int x
).
Argument
The actual value of a parameter (e.g.: in methodOne(5)
, the argument passed as variable x
is 5
).
Additional Language Resources
Python Class and Instance Variables
30天代码day4 Class vs. Instance的更多相关文章
- 30行代码搞定WCF并发性能测试
[以下只是个人观点,欢迎交流] 30行代码搞定WCF并发性能 轻量级测试. 1. 调用并发测试接口 static void Main() { List< ...
- 30行代码让你理解angular依赖注入:angular 依赖注入原理
依赖注入(Dependency Injection,简称DI)是像C#,java等典型的面向对象语言框架设计原则控制反转的一种典型的一种实现方式,angular把它引入到js中,介绍angular依赖 ...
- 30行代码实现Javascript中的MVC
从09年左右开始,MVC逐渐在前端领域大放异彩,并终于在刚刚过去的2015年随着React Native的推出而迎来大爆发:AngularJS.EmberJS.Backbone.ReactJS.Rio ...
- Tensorflow快餐教程(1) - 30行代码搞定手写识别
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/lusing/article/details ...
- 10分钟教你用python 30行代码搞定简单手写识别!
欲直接下载代码文件,关注我们的公众号哦!查看历史消息即可! 手写笔记还是电子笔记好呢? 毕业季刚结束,眼瞅着2018级小萌新马上就要来了,老腊肉小编为了咱学弟学妹们的学习,绞尽脑汁准备编一套大学秘籍, ...
- JAVA_SE基础——30.构造代码块
黑马程序员入学blog...构造代码块作用:给所有的对象进行统一的初始化. 问题:要求每个小孩出生都会哭,这份代码有两个构造函数,如果需要每个小孩出生都要哭的话,那么就需要在不同的构造函数中都调用cr ...
- 30天代码day0
a class is a collection of variables (fields) and functions called methods. A program is a collectio ...
- 30 行代码实现 JS 中的 MVC
一连串的名字走马观花式的出现和更迭,它们中一些已经渐渐淡出了大家的视野,一些还在迅速茁壮成长,一些则已经在特定的生态环境中独当一面舍我其谁.但不论如何,MVC已经并将持续深刻地影响前端工程师们的思维方 ...
- 30行代码消费腾讯人工智能开放平台提供的自然语言处理API
腾讯人工智能AI开放平台上提供了很多免费的人工智能API,开发人员只需要一个QQ号就可以登录进去使用. 腾讯人工智能AI开放平台的地址:https://ai.qq.com/ 里面的好东西很多,以自然语 ...
随机推荐
- 大量数据的excel导出
对于大型excel的创建且不会内存溢出的,就只有SXSSFWorkbook了.它的原理很简单,用硬盘空间换内存(就像hash map用空间换时间一样). private void writeToAla ...
- Python2.x爬虫入门之URLError异常处理
大家好,本节在这里主要说的是URLError还有HTTPError,以及对它们的一些处理. 1.URLError 首先解释下URLError可能产生的原因: (1)网络无连接,即本机无法上网 (2)连 ...
- 使用AES加密的勒索类软件分析报告
报告名称: 某勒索类软件分析报告 作者: 李东 报告更新日期: 样本发现日期: 样本类型: 样本文件大小/被感染文件变化长度: 样本文件MD5 校验值: da4ab5e31793 ...
- @Autowired注解与@Qualifier注解搭配使用
问题:当一个接口实现由两个实现类时,只使用@Autowired注解,会报错,如下图所示 实现类1 实现类2 controller中注入 然后启动服务报错,如下所示: Exception encount ...
- spring cloud 随笔记录(1)-
最近随着微服务的火热,我也开始对我服务进行了解了 毕竟程序员这一行需要及时更新自己的技能,才能更好的生存. 我理解的微服务 项目由多个独立运行的程序组成,每个服务运行在自己的进程中,服务间采用轻量 ...
- 自定义Hook
在 class RegForm(form.Form) 中 1.验证两次密码是否相同 from django.core.exceptions import ValidationError def cle ...
- AJAX的简单示例:注册校验
众所周知,我们每次需要注册一个网站的用户名时,都会校验该邮箱.用户名是不是正确的格式.是不是有被使用过,密码是否符合规则,二次确认是否符合. 如果这些校验都采用form表单提交的话,会给用户带来极不好 ...
- Python基础(切片,list循环,元组)
list和字符串循环: 切片:list取值的一种方式,同样适用于字符串(因为字符串也有下标) 不可变类型:元组,字符串
- 关于redis实现分布式锁
前言 分布式锁一般有三种实现方式:1. 数据库乐观锁:2. 基于Redis的分布式锁:3. 基于ZooKeeper的分布式锁.本篇博客将介绍第二种方式,基于Redis实现分布式锁.虽然网上已经有各种介 ...
- oraclesql语句笔记
1. ORA-00947:Not enough values 原因:values没有写足够的值与select()中的字段对应 2.查看一张表中共有多少个字段 select count(*) from ...