1.对象与类
类是一个标准,是需要被定义的,就跟int的一样,int是java已经被定义的一个类,现在我们也能定义一个类。
而每一个对象是类的具体体现,用来做事,类定义了一所有对象长什么样,而对象是类的一个个具体的实例。
EG:
对象: 属性或者状态+ 服务
数据:属性或者状态
操作:函数
里面是蛋黄,外面是 蛋白。
数据 操作
封装:数据和数据的操作放在一个地方
人只能通过外面的操作要求对象做点事情,提供点服务,可是内部的数据应该是由对象本身所保护着的。
2.举个例子,方便理解。
/如果你看一个东西,要看它有些什么东西,这些东西能做什么动作,提供什么服务,这是面向对象的思想。
如:VendingMechine
2.1.1
创建对象,和数组长的有点像,对象变量是对象的管理者。
new VendingMechine();
VendingMechine v =new VendingMechine()
这里有price balance total这些属性, 要做show prompt insertMoney getfood 这些动作。
public class VendingMechine {
// 如果看一个东西,它有些什么东西,都能做什么动作,提供什么服务
//属性
int price=50;
int balance;
int total;
//动作
void showprompt()
{
System.out.println("Welcome !"+"请输入金额:");
}
void insertMoney(int money)
{
balance+=money;
}
void showbalance()
{
System.out.println(balance);
}
void getfood()
{
if(balance>=price)
{
System.out.println("Here you are!");
}
}
void total()
{ if(balance>=price)
{
balance-=price;
total+=(balance-price);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//做动作
VendingMechine a=new VendingMechine();
a.showprompt();
a.insertMoney(100);
a.showbalance();
a.getfood();
a.total();
VendingMechine b=new VendingMechine();
b.insertMoney(200);
b.total();
b.showbalance();
a.showbalance();
// 法一:a,b两个管理的是不同的对象,
/*Welcome !请输入金额:
100
Here you are!
150
50
*/
// 法二:
debugc查ID
//当使用b=a时,b就管理了a的对象。
/*Welcome !请输入金额:
100
Here you are!
50
50
*/
}
}
debug查对象的ID:
3. 成员变量和成员方法。
3.1调用函数
通过 . 运算符来调用某个对象的函数
刚刚看了下前面的笔记
发现这样也能调用函数,并且不用创照对象,在主方法里面就能调用,因为我用了static静态的常量,如果去掉就要创建对象来调用
public class xiugai1 {
public static void sum(int a,int b)
{ int i;
int sum=0;
for(i=a;i<=b;i++)
{
sum+=a;
}
System.out.println(a+"到"+b+"的和是"+sum);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
sum(5,6);
//去掉
public void sum(int a,int b)
{ int i;
int sum=0;
for(i=a;i<=b;i++)
{
sum+=a;
}
System.out.println(a+"到"+b+"的和是"+sum);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
xiugai1 a=new xiugai1();
a.sum(5,6);
}
}
int balance;
void setprice()
{
showbalance();/*或者 this.showbalance*/
}
void showbalance()
{
System.out.println(balance);
}
3.1.2本地变量和成员变量
本地变量:进入函数,这个变量就有,离开就不存在了。
成员变量:当你new出一个对象后成员变量才存在,当很久没用这个对象后之后,java会自动清除。
总结:
1 .当我们说作用域在类的内部,实际意思是:
这个类成员函数可以使用成员变量,成员变量初始化的时候也可以使用这些成员变量。
2 . this:
this是成员函数的一个固有的本地变量,在成员函数内部调用其他成员函数 用this
当我们设计一个类,制造了一个对象,我们通过 . 运算符来调用函数,如果是在成员函数内部,我们就通过this.函数名()或者 函数名()来调用,不需要通过对象。
而在成员函数的外部,需要用对象的名字去调用成员函数。
int price;
/*函数*/void setprice(int price)
{
price=price
//无论怎么price,都是指这个参数(函数的本地变量的)price
//而用this,price,就是指成员变量price.
this.price
}
int balance=0;
void insertMoney(int money)
{
balance+=money;
showbalance();
}
void showbalance()
{
System.out.println(this.balance);
}
当我们在showbalance中用this.balance,上面在insrtMoney中的balance被赋值的情况就会被加进输出里面,而不是0了
20.12.15补充
当局部变量和成员变量的名字相同时,可以用this来区分
通过谁来调用,谁就是this.
package 函数;
import java.util.Scanner;
public class 函数1 {
String name="钢铁侠";
public void name1(String name)
{
System.out.println("你好"+name+",我是"+this.name);
/*a*/ System.out.println(this);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
函数1 a=new 函数1();
a.name1("蝙蝠侠");
//谁调用了这个方法,谁就是this
/*b*/ System.out.println(a);
}
具体结果
//你好蝙蝠侠,我是钢铁侠
/*a*/ //函数.函数1@7291c18f
/*b*/ //函数.函数1@7291c18f
}
再来一遍
当一个包有多个类时候,任意一个类都可以调用其余类里面的成员变量和成员方法。
1.成员方法和成员变量
public class Students {
//成员方法不能用static
//成员变量定义在方法外边,类里面
/*1.导包 import 函数.函数1 Students;
若我使用的Students类和我自己的函数1类位于同一个包下就不用导包了(函数1已经导包的情况下)
2.创建对象: 类名称 对象=new 类名称()
3.使用
3.1使用成员变量
对象名.成员变量名。
对象名.成员方法名.
*/
String name;
int age;
/*
3.2成员方法
*/
public void eat()
{
System.out.println("吃饭饭");
}
public void sleep()
{
System.out.println("睡觉觉");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Students a=new Students();
a.name="lbwnb";
a.age=100;
System.out.println(a.name);
System.out.println(a.age);
/*
3.2.1使用成员方法
对象名.成员方法名。
*/
a.sleep();
a.eat();
}
}
注释1对应的图片:
2.具体的例子
//写一个类,来模拟iPhone
/*属性:128g,3个摄像头,石墨色,OLED屏幕
行为:隔空投送,摄影,上微信,打游戏,计算机
*/
public class iphone {
//成员变量
int tou=3;
int daxiao=128;
String color="石墨色";
String curtain="OLED屏幕";
//成员方法
public void wechat()
{ int a=2,b=1;
System.out.println(a+b);
}
public void game(String who)
{
System.out.println("打开"+who);
}
public void gekong()
{
System.out.println("打开隔开投送");
}
public static void main(String[] args) {
//写一个类,来模拟iPhone
/*属性:128g,3个摄像头,石墨色,OLED屏幕
行为:隔空投送,摄影,上微信,打游戏,计算机
*/
iphone a=new iphone();
a.color="蓝色";
a.daxiao=256;
System.out.println(a.color);
System.out.println(a.daxiao);
}
}
3.把对象作为参数,传递到方法中。
//注意:当一个对象作为参数,传递到方法当中时,实际上传递的是地址的值。
public class iphone2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
iphone one=new iphone();
one.color="灰色的";
one.daxiao=100;
method(one);//名为method的方法调用one这个对象的地址值,而在iphone这个类中已经创建了 color和daxiao这两个成员变量。
//换句话说就是方法mathod得到了iphone这个类的地址值
}
public static void method(iphone a) //调用的是iphone类型的a对象
//用自定义的一个类作为method的参数。
{
System.out.println(a.color);
System.out.println(a.daxiao);
}
}
4.使用对象类型作为方法的返
//当使用一个对象的类型作为方法的返回值时,返回值就是对象的地址值。
public class iphone3 {
//iphone类型的对象 getphone
public static iphone getphone ()
{
iphone a=new iphone();
a.color="红色";
a.price=9998;
return a; //a的类的地址作为返回值。
//谁调用了我,我就把iphone的地址交给谁,下面主方法调用了名为getphone的成员方法
}
public static void main(String[] args) {
// TODO Auto-generated method stub
iphone b=getphone();
System.out.println(b.color);
System.out.println(b.daxiao);
System.out.println(b.price);
}
//当使用一个对象的类型作为方法的返回值时
//返回值就是对象的地址值。
}
5.成员变量和局部变量的区别
5.1
定义的位置不同
局部变量:方法的内部
成员变量: 方法外,类当中
5.2
作用范围不同
局部变量:只有方法中才能使用
成员变量: 整个类里面都可以通用
5.3默认值不一样
局部变量:没有默认值;
成员变量: 没有赋值会有默认值,和数组一样。