变量的作用域
JAVA中用类来组织程序,类中可以定义成员变量和成员方法。
变量作用域是指可在程序中按变量名访问该变量的区域。变量的作用域和与变量的声明位置有关,不同的声明位置决定了变量不同的作用域。
在类中定义的变量称为类的成员变量,在方法中定义的变量称为局部变量,局部变量的作用区域只是在定义它的方法内。
封装的概念
封装是指将东西包装在一起,然后以新的完整形式呈现,对于OOP而言,封装是将方法和属性一起包装到程序单元中,这些单元以类的形式实现。
在oop中,每当定义类/对象时,往往会将相关相互关联的数据和功能绑定在一起,用计算机专业术语讲,隐藏属性、方法和实现的详细信息的处理方式称为封装。
封装的好处之一是隐藏信息,"隐藏信息"是一个强大的技术工具,它能降低程序的复杂性,在创建类时,会根据完成任务的需要创建许多属性和方法,而只有被其他程序访问的那些属性和方法才对外公开。
this关键字和static关键字
关键字static可应用于变量方法,甚至不属于方法的一段代码块。
方法声明为静态时,具有某些限制
(1)他们只能调用其他静态方法。
(2)他们必须只访问静态数据。
(3)不能使用关键字this和super。
创建和使用对象
Person类定义好了以后,就可以根据定义成员变量和成员方法。
代码片
package com.hg.day07;
public class demo01 {
public static void main(String[] args) {
int age1=20;
test01 var1=new test01();
var1.methodA();
System.out.println("================");
var1.name="袁雪华";
var1.age=18;
System.out.println(var1.name);
System.out.println(var1.age);
System.out.println("================");
var1.methodB();
}
}
package com.hg.day07;
public class Demo02Method {
//给一个数组,返还最大值
public static int getMax(int[] array) {
int max=array[0];
for(int i=1;i<array.length;i++) {
if(array[i]>max) {
max=array[i];
}
}
return max;
}
}
代码片
package com.hg.day07;
public class Demo02MethodUse {
public static void main(String[] args) {
int[] array= {5,15,25,500,20,100};
int max=Demo02Method.getMax(array);
System.out.println("最大值:"+max);
}
}
package com.hg.day07;
public class Demo03Person {
public static void main(String[] args) {
Person person=new Person();
person.setAge(19);
person.getName("袁雪华");
person.setMale(false);
person.show();
if(person.isMale()==true)
{
System.out.println("男性");
}else {
System.out.println("女性");
}
}
}
代码片
package com.hg.day07;
public class Demo04Student {
public static void main(String[] args) {
student student1=new student();
student1.setAge(18);
student1.setName("袁雪华");
student1.show();
System.out.println("================");
student student2=new student();
student2.setAge(19);
student2.setName("小龙");
student2.show();
student1.show();
}
}
package com.hg.day07;
public class Person {
private String name;//姓名
private double age;//年龄
private boolean male;//是否为男性 true 1字节
public void setMale(boolean boy) {
male = boy;
}
public boolean isMale() {
return male;
}
public void setAge(double num) {
if(num < 200 && num >=0) {
age=num;
}else{
System.out.println("数据不合适");
}
}
public double getAge() {
return age;
}
public void getName(String who) {
name=who;
}
public String getName() {
return name;
}
public void show() {
System.out.println("我"+ name +"年龄"+age);
}
}
代码片
package com.hg.day07;
public class student {
String name;
double age;
public void eat() {
System.out.println("吃饭");
}
public void sleep() {
System.out.println("睡觉");
}
public void study() {
System.out.println("学习");
}
public void show() {
System.out.println("我叫"+name+"年龄"+age);
}
public void setName(String who) {
name=who;
}
public void setAge(double num) {
if(num<200&&num>=0) {
age=num;
}else {
System.out.println("资源不合理!");
}
}
}
package com.hg.day07;
public class test01 {
String name;
int age;
public void methodA() {
int age2=66;
System.out.println(name);
System.out.println(age);
System.out.println(age2);
}
public void methodB() {
age=30;
System.out.println(age);
}
}
成果展示