对象调用过程:
1.输入参数;
2.等待目标方法执行结束;
3.返回结果
Java中,引入对象和类的概念
类型(是规范、是定义)
,从万千对象中抽取共性
public class OPExample {
public static void main(String[] args) {
int a, b, c;
a = 1;
b = 2;
c = add(a, b);
System.out.println("c is " + c);
}
// 函数定义
public static int add(int m, int n) {
return m + n;
}
}
方法是主体,所有的参数是类似于宾语.
public class OOExample {
private int a;
public void setA(int a) {
this.a = a;
}
public int add(int b) {
return this.a + b;
}
public static void main(String[] args) {
int b = 5;
OOExample obj = new OOExample();
obj.setA(10);
System.out.println(obj.add(b));
}
}
方法从属于一个对象,而这个对象可以执行自身的某个方法.
obj.add(b):主语obj,谓语add,宾语b
short和int都是整数类型,但是没有继承关系,只是表示的整数范围不同而已。
int a; float b;
// c语言结构体不能包含函数
struct Simple{
int a;
double b;
};//成员默认均为public
struct Complex{
char c;
struct Simple foo;
//Complex必须包含Simple所有内容
};
Complex包含:int a,float b,char c
public class Father {
private int money=100;//私有
long mobile=1399999999L;
public void hello(){
System.out.println("hello");
}
}
//--------------------------------------------
public class Son extends Father {
public void hi(){
//子类可以扩展自己的成员方法
System.out.println("hi !!!!!");
}
public static void main(String[] args){
Son s=new Son();
System.out.println(s.mobile);//Son没有定义mobile,而是通过父类继承的
// System.out.println(s.money);//error 父类的money是私有的,子类无法直接访问
s.hello();// Son没有定义f1,而是通过父类继承的
s.hi();//Son可以自定义自己的成员方法
}
}
子类必须通过父类的方法才能访问父类的私有变量。
// MyNumber
class MyNumber
{
int num = 5;
}
// ReferenceTest
public class ReferenceTest {
public static void main(String[] args) {
int num1 = 5;
int num2 = num1;
System.out.println("num1: " + num1 + ", num2: " + num2);
num2 = 10;
System.out.println("num1: " + num1 + ", num2: " + num2);
MyNumber obj1 = new MyNumber();
MyNumber obj2 = new MyNumber();
System.out.println(obj1.num);
System.out.println(obj2.num);
System.out.println("======接下来输出新值ֵ=====");
obj2 = obj1;
obj2.num = 10;
System.out.println(obj1.num);
System.out.println(obj2.num);
}
}
// ArgumentPassingTest
public class ArgumentPassingTest {
public static void main(String[] args) {
int a = 1, b = 2;
swap(a,b);
System.out.println("a is " + a + ", b is " + b); //a=1, b=2
MyNumber obj1 = new MyNumber();
MyNumber obj2 = new MyNumber();
obj2.num = 10;
swap(obj1, obj2);
System.out.println("obj1 is " + obj1.num + ", obj2 is " + obj2.num); // obj1 10, obj2 5
}
// int型是直接赋值的
public static void swap(int m, int n)
{
int s = m;
m = n;
n = s;
}
public static void swap(MyNumber obj3, MyNumber obj4)
{
int s = obj3.num;
obj3.num = obj4.num;
obj4.num = s;
}
}
调用swap(a,b);在内存里出现a和b,以及m和n,都是单独的数字,m和n的交换不影响a和b
public class Initialization {
boolean v1;
byte v2;
char v3;
double v4;
float v5;
int v6;
long v7;
short v8;
public static void main(String[] args) {
Initialization obj = new Initialization();
System.out.println("The initial value of boolean variable is " + obj.v1);
System.out.println("The initial value of byte variable is " + obj.v2);
System.out.println("The initial value of char variable is " + obj.v3); //\u0000 space
System.out.println("The initial value of double variable is " + obj.v4);
System.out.println("The initial value of float variable is " + obj.v5);
System.out.println("The initial value of int variable is " + obj.v6);
System.out.println("The initial value of long variable is " + obj.v7);
System.out.println("The initial value of short variable is " + obj.v8);
int a;
// System.out.println(a); //error, a 没有初始化
}
}
public class A {
int id;
public A(int id2) {
id = id2;
}
}
构造函数没有返回值
public void A(int id2)不是构造函数,而实普通函数
Java构造函数的名称必须和类名一样,且没有返回值
public class LifeCycleTest {
public static void main(String[] args) {
int a=0; // a 在main函数中都是active
//i只能活在for循环中
for(int i=0; i<5; i++) {
int k = 0;
k++;
}
if(a>0) {
Object obj1 = new Object(); //obj1 只能在if分支中
System.out.println("a is positive");
} else {
Object obj2 = new Object(); //obj2 只能else分支中
System.out.println("a is non-positive");
}
}
}
每一个变量,都只能存活于离它最近的一对大括号当中
所以不需要析构函 数来释放内存
。垃圾回收器GC
(Garbage Collector),自动
为该空的无形参
构造函数。如果已经有了显函数重载(overload),函数名相同,形参不同
class MyPairNumber
{
int m;
int n;
public MyPairNumber()
{
m = 0;
n = 0;
}
public MyPairNumber(int a)
{
m = a;
n = 0;
}
public MyPairNumber(int a, int b)
{
m = a;
n = b;
}
}
public class ConstructorTest {
public static void main(String[] args) {
MyPairNumber obj1 = new MyPairNumber();
MyPairNumber obj2 = new MyPairNumber(5);
MyPairNumber obj3 = new MyPairNumber(10,20);
System.out.println("obj1 has " + obj1.m + "," + obj1.n);
System.out.println("obj2 has " + obj2.m + "," + obj2.n);
System.out.println("obj3 has " + obj3.m + "," + obj3.n);
// A a = new A(); //error, A类中没有无参数的构造函数
}
}
多个class可以写在一个.java文件中,但是最多只能由一个类时public class,并且public class类的名字必须和.java的文件名相同
如果类由构造函数,编译器不会自动增加无参的空的构造函数
信息隐藏
// InfoHiding
class InfoHiding {
private int id;
public InfoHiding(int id2) {
id = id2;
}
public int getId() {
return id;
}
public void setId(int id2) {
id = id2;
}
}
// InfoHidingTest
public class InfoHidingTest {
public static void main(String[] args) {
InfoHiding obj = new InfoHiding(100);
obj.setId(200);
System.out.println(obj.getId());
}
}
信息隐藏:通过类的方法来间接访问类的属性,而不是直接访问类的属性
public class Person {
private int height;
private int weight;
private int age;
private String name;
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
// InfoHiding
class InfoHiding {
private int id;
public InfoHiding(int id2) {
id = id2;
}
public int getId() {
return id;
}
public void setId(int id2) {
id = id2;
}
}
// InfoHiding2
public class InfoHiding2 {
private int id;
public InfoHiding2(int id)
{
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
// ThisTest
public class ThisTest {
public static void main(String[] args) {
MyPairNumber obj = new MyPairNumber(5);
System.out.println(obj.sum());
}
}
// MyPairNumber
public class MyPairNumber {
private int m;
private int n;
public int getM() {
return m;
}
public void setM(int m) {
this.m = m;
}
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
public MyPairNumber() {
this(0, 0);
}
public MyPairNumber(int m) {
this(m, 0);
}
public MyPairNumber(int m, int n) {
this.m = m;
this.n= n;
}
public int sum() {
return this.add(m,n); //return add(m,n); also ok
}
public int add(int a, int b){
return a+b;
}
}
this:指向本类中的成员变量;指向本类中的成员方法;可以当作构造函数使用