[Training Video - 3] [Java Introduction] [Object Oriented Programming]
Static and non-Static :
非静态方法(不带static)可以访问静态变量和方法(带static),但是反过来就不行。
类的静态成员(变量和方法)属于类本身,在类加载的时候就会分配内存,可以通过类名直接去访问。
非静态成员(变量和方法)属于类的对象,只有在类的对象产生(创建实例)的时候才会分配内存,然后通过类的对象去访问。
在一个类的静态成员中去访问非静态成员之所以会出错是因为在类的非静态成员不存在的时候静态成员就已经存在了,访问一个内存中不存在的东西会出错。

静态变量和方法既可以用类来调用,也可以用对象来调用,但不建议用对象调用
Code :
public class Car {
// GLOBAL VARIABLE
String mod;
int price;
static int wheels = 4;
// static functions can only access static stuff
public static void main(String[] args) {
Car c1 = new Car();
c1.mod = "Merc";
c1.price = 8909809;
c1.start();
c1.accel();
Car c2 = new Car();
c2.mod = "Maruti";
c2.price = 8909809;
c2.start();
c2.accel();
System.out.println(c1.mod);
System.out.println(c2.mod);
// static
System.out.println(wheels);
System.out.println(Car.wheels);
c1.wheels = 8;
System.out.println(wheels);
System.out.println(Car.wheels);
System.out.println(c2.wheels);
fillGas(100);
Car.fillGas(200);
c1.fillGas(900);
}
public void start(){
System.out.println(mod + " starting");
}
public void accel(){
System.out.println(mod + " acc");
}
public static void fillGas(int quantity){
}
}
Result :
Merc starting
Merc acc
Maruti starting
Maruti acc
Merc
Maruti
4
4
8
8
8
Code :
public class Car {
String mod;
public static void main(String[] args) {
Car a = new Car();
Car b = new Car();
Car c = new Car();
a.mod = "A";
b.mod = "B";
c.mod = "C";
System.out.println(a.mod);
System.out.println(b.mod);
System.out.println(c.mod);
a=b;
b=c;
c=a;
System.out.println(a.mod);
System.out.println(b.mod);
System.out.println(c.mod);
}
}
Result :
A
B
C
B
C
B
[Training Video - 3] [Java Introduction] [Object Oriented Programming]的更多相关文章
- [Training Video - 2] [Java Introduction] [Operator, Loops, Arrays, Functions]
Operator : While Loop : For Loop : Arrays : Code : public class FirstJavaClass { public static void ...
- [Training Video - 2] [Java Introduction] [Install Java and Eclipse, Create Class]
Download Java : https://java.com/en/download/ Download Eclipse : https://www.eclipse.org/downloads/ ...
- Object Oriented Programming python
Object Oriented Programming python new concepts of the object oriented programming : class encapsula ...
- JavaScript: Constructor and Object Oriented Programming
Constructor : Grammar: object.constructor Example: Javascript code: 1 function obj1() { this.number ...
- 面对对象编程(OOP, Object Oriented Programming)及其三个基本特性
一千个读者,一千个哈姆雷特.对于面对对象编程,书上都会告诉我们它有三个基本特性,封装,继承,多态,但谈起对这三点的见解,又是仁者见仁智者见智,感觉还是得多去编程中体验把 . 面向对象编程(OOP, O ...
- Java - 面向对象(object oriented)计划 详细解释
面向对象(object oriented)计划 详细解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/24058107 程序包括 ...
- python, 面向对象编程Object Oriented Programming(OOP)
把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 面向过程的程序设计把计算机程序视为一系列的命令集合,即一组函数的顺序执行.为了简化程序设计,面向过程把函数继续切分为子函数,即把大块函数 ...
- Python 面向導向語言 Object Oriented Programming Language
Pytho 是面向對象的程式語言,舉凡 Literals 值都是 Object.例如: >>> id(38)8791423739696 與 >>> id('ABC' ...
- leetcode@ [355] Design Twitter (Object Oriented Programming)
https://leetcode.com/problems/design-twitter/ Design a simplified version of Twitter where users can ...
随机推荐
- Codeforces 868F. Yet Another Minimization Problem【决策单调性优化DP】【分治】【莫队】
LINK 题目大意 给你一个序列分成k段 每一段的代价是满足\((a_i=a_j)\)的无序数对\((i,j)\)的个数 求最小的代价 思路 首先有一个暴力dp的思路是\(dp_{i,k}=min(d ...
- 51nod 算法马拉松4 D装盒子(网络流 / 二分图最优匹配)
装盒子 基准时间限制:1 秒 空间限制:131072 KB 分值: 160 有n个长方形盒子,第i个长度为Li,宽度为Wi,我们需要把他们套放.注意一个盒子只可以套入长和宽分别不小于它的盒子,并且 ...
- QLoo graphql engine 学习二 基本试用(kubernetes)
已经测试过docker&& docker-compose 的运行模式,下面测试下kubernetes的运行模式 kubernetes 我使用docker for mac qloo 安装 ...
- 提高ASP.NET页面载入速度的方法
前言 本文是我对ASP.NET页面载入速度提高的一些做法,这些做法分为以下部分: 目录 1.采用 HTTP Module 控制页面的生命周期. 2.自定义Response.Filter得到输出流str ...
- Javascript 的数据是什么数据类型?
Javascript 中的数据都是以 64 位浮点 float 存储的. 所有语言对浮点的精度是很难确定的. 如下代码可以实验到问题. <script> var a = 0.4; var ...
- 国内yum源的安装(163,阿里云,epel)
----阿里云镜像源 1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 2.下载新的 ...
- CCFlow SDK模式开发(有比较详细的代码,以服务的形式与ccflow数据库进行数据交互)
http://www.cnblogs.com/s0611163/p/3963142.html 需求: 1.业务数据要保存在我们自己的数据库里 2.CCFlow有保存草稿的功能,但是领导要求每个 ...
- 玩转Panabit 2008 Live CD到U盘,Panabit随身行
直接将Panabit 2008 Live CD的内容复制到U盘,即把Live CD转成U盘启动盘,U盘可写,方便保存配置.要能正常使用,必须机器支持USB启动,才能用上:但是熟悉此方法,同样可以灵活用 ...
- Python——内置函数(待完善)
内置函数(68个),分为六大类 思维导图: 1. 迭代器/生成器相关(3个) (1)range for i in range(10): #0-9 print(i) for i in range(1,1 ...
- 【nodeJS】webstorm中设置nodej智能提示