Understanding of object-oriented practice 1
When using a cell phone
Enter the number in the phone dial page, and then dial the phone, which is process-oriented
Address book to find contacts in the phone and dial the phone, which is an object-oriented
Both have advantages and disadvantages idea, not object-oriented best.
In some cases, better use of process-oriented, such as our previous practice, so easy to understand learning.
As in everyday life, we will use the same process-oriented to make a call. Some important calls such as 110,119,120.
Package com.cnblogs.www.demo8.test1;
public class Dog { // class, having the same characteristics and behavior of the object set
int size; // instance variable size indicates the size, size
String breed; // instance variable represented by breed type
String name; // instance variable name represents a name
}
Package com.cnblogs.www.demo8.test1;
public class DogTest {
public static void main (String [] args) {
// a single class as a template to create three objects simultaneously and each object.
// do not get to the bottom, simple to understand because each object class to maintain its own copy of an instance variable.
// There needs to change, then use the object name to call after you create an object in the form of change.
D = Dog new new Dog ();
d.size = 40; // modify instance variables
System.out.println (d.size);
Dog E = new new Dog ();
e.size= 60; // modify instance variables
System.out.println (e.size);
Dog F = new new Dog ();
f.size = 80; // modify instance variables
System.out.println (f.size);
}
}
Class 2 Exercise distinguish variables, instance variables, local variables
package com.cnblogs.www.demo8.text2;
public class Demo {
static int a = 1; //类变量:独立于方法之外,使用static修饰,也称静态变量,多用于声明常量
public int b = 2; //实例变量:独立于方法之外,在实体类中被称为属性
public void c() {
int d = 1; //局部变量:只在方法中有效
//由于局部变量只存在于方法中,所以想要打印输出,可以先在方法中打印输出,然后通过对象调用这个方法
System.out.println("局部变量: " +d);
}
}
package com.cnblogs.www.demo8.text2;
public class DemoTest {
public static void main(String[] args) {
Demo o = new Demo(); //声明一个对象并初始化实例
System.out.println("实例变量: "+ o.b);
o.c(); //调用类中的方法
}
}