6.1类与对象的定义
6.1.1 类的定义
代码语言:javascript复制jshell> class Person {
...> //属性
...> public String name; //姓名
...> public char sex; //性别
...> public int age; //年龄
...> //方法
...> public void speak(){ //说话
...> System.out.println("姓名:" name ",性别:" sex ",年龄:" age);
...> }
...> }
| 已创建 类 Person
6.1.2 创建对象
代码语言:javascript复制jshell> Person p=new Person(); //定义对象
p ==> Person@6f1fba17
jshell> p.name="张三"; //访问属性
$34 ==> "张三"
jshell> p.sex='女';
$35 ==> '女'
jshell> p.age=20;
$36 ==> 20
jshell> p.speak(); //访问方法
姓名:张三,性别:女,年龄:20
jshell>
创建多个对象
代码语言:javascript复制jshell> Person q=new Person(); //定义对象
q ==> Person@5ae63ade
jshell> q.name="李四"; //访问属性
$39 ==> "李四"
jshell> q.sex='男';
$40 ==> '男'
jshell> q.age=21;
$41 ==> 21
jshell> q.speak(); //访问方法
姓名:李四,性别:男,年龄:21
jshell>
对象引用的传递
代码语言:javascript复制jshell> p=new Person(); //定义对象
p ==> Person@2b552920
jshell> p.name="王五"; //访问属性
$44 ==> "王五"
jshell> p.sex='男';
$45 ==> '男'
jshell> p.age=19;
$46 ==> 19
jshell> p.speak(); //访问方法
姓名:王五,性别:男,年龄:19
jshell>
6.2 封装性
6.2.1 存在问题
代码语言:javascript复制jshell> p.sex='女';
$48 ==> '女'
jshell> p.speak();
姓名:王五,性别:女,年龄:19
6.2.2 private属性
代码语言:javascript复制jshell> class Person {
...> //属性
...> private String name; //姓名
...> private char sex; //性别
...> private int age; //年龄
...> //方法
...> public void speak(){ //说话
...> System.out.println("姓名:" name ",性别:" sex ",年龄:" age)
;
...> }
...> }
| 已替换 类 Person
| 更新已替换 变量 q, 重置为空值
| 更新已替换 变量 p, 重置为空值
jshell>
代码语言:javascript复制jshell> p.sex='女';
| 错误:
| sex 在 Person 中是 private 访问控制
| p.sex='女';
| ^---^
jshell>
6.2.3 访问控制
代码语言:javascript复制jshell> class Person {
...> //属性
...> private String name; //姓名
...> private char sex; //性别
...> private int age; //年龄
...> //普通方法
...> public void speak(){ //说话
...> System.out.println("姓名:" name ",性别:" sex ",年龄:" age)
;
...> }
...> //以下是访问控制方法
...> public String getName() {
...> return name;
...> }
...> public void setName(String name) {
...> this.name = name;
...> }
...> public char getSex() {
...> return sex;
...> }
...> public void setSex(char sex) {
...> this.sex = sex;
...> }
...> public int getAge() {
...> return age;
...> }
...> public void setAge(int age) { //写年龄控制
...> if(age>=1&&age<=120)
...> this.age = age;
...> else
...> System.out.println("age参数有误");
...> }
...> }
| 已替换 类 Person
| 更新已替换 变量 q, 重置为空值
| 更新已替换 变量 p, 重置为空值
jshell>
正常访问
代码语言:javascript复制jshell> Person p=new Person();
p ==> Person@22a67b4
jshell> p.setName("张三");
jshell> p.setSex('女');
jshell> p.setAge(20);
jshell> p.speak();
姓名:张三,性别:女,年龄:20
jshell>
代码语言:javascript复制jshell> p.setAge(500);
age参数有误
jshell> p.speak();
姓名:张三,性别:女,年龄:20
jshell>
6.3 构造器
代码语言:javascript复制jshell> class Person {
...> //属性
...> private String name; //姓名
...> private char sex; //性别
...> private int age; //年龄
...> //构造器
...> public Person(){
...> System.out.println("默认无参构造器被调用!");
...> }
...> public Person(String name,char sex,int age){
...> System.out.println("有参构造器被调用!");
...> this.name=name;
...> this.sex=sex;
...> this.age=age;
...> }
...>
...> //普通方法
...> public void speak(){ //说话
...> System.out.println("姓名:" name ",性别:" sex ",年龄:" age
);
...> }
...> //以下是访问控制方法
...> public String getName() {
...> return name;
...> }
...> public void setName(String name) {
...> this.name = name;
...> }
...> public char getSex() {
...> return sex;
...> }
...> public void setSex(char sex) {
...> this.sex = sex;
...> }
...> public int getAge() {
...> return age;
...> }
...> public void setAge(int age) { //写年龄控制
...> if(age>=1&&age<=120)
...> this.age = age;
...> else
...> System.out.println("age参数有误");
...> }
...> }
| 已替换 类 Person
| 更新已替换 变量 q, 重置为空值
| 更新已替换 变量 p, 重置为空值
jshell>
代码语言:javascript复制jshell> Person p=new Person();
默认无参构造器被调用!
p ==> Person@7c29daf3
jshell> Person q=new Person("刘六",'女',22);
有参构造器被调用!
q ==> Person@9660f4e
jshell> q.speak();
姓名:刘六,性别:女,年龄:22
jshell>
6.4 匿名对象
代码语言:javascript复制jshell> new Person("匿名对象",'男',18).speak();
有参构造器被调用!
姓名:匿名对象,性别:男,年龄:18
jshell>
6.5 JavaBean
代码语言:javascript复制jshell> class Point{
...> private double x;
...> private double y;
...> public Point(){}
...> public Point(double x,double y){
...> this.x=x;
...> this.y=y;
...> }
...> public double distance(Point p){
...> double d=(this.y-p.getY())*(this.y-p.getY());
...> d=d (this.x-p.getX())*(this.x-p.getX());
...> d=Math.sqrt(d);
...> return d;
...> }
...> public double getX() {
...> return x;
...> }
...> public void setX(double x) {
...> this.x = x;
...> }
...> public double getY() {
...> return y;
...> }
...> public void setY(double y) {
...> this.y = y;
...> }
...> }
| 已创建 类 Point
jshell> Point p=new Point();
p ==> Point@5622fdf
jshell> p.setX(0)
jshell> p.setY(0)
jshell> Point q=new Point(1,1);
q ==> Point@42e26948
jshell> p.distance(q)
$14 ==> 1.4142135623730951
jshell>
jshell>
6.6 方法重载
代码语言:javascript复制jshell> class Maths{
...> //计算圆的面积
...> public double area(double r){
...> return 3.14*r*r;
...> }
...> //计算椭圆的面积
...> public double area(double a, double b){
...> return 3.14*a*b;
...> }
...> }
| 已创建 类 Maths
jshell> new Maths().area(1);
$65 ==> 3.14
jshell> new Maths().area(2,3);
$66 ==> 18.84
jshell>
6.7 静态成员
代码语言:javascript复制jshell> class Maths{
...> //计算圆的面积
...> public static double area(double r){
...> return 3.14*r*r;
...> }
...> //计算椭圆的面积
...> public static double area(double a, double b){
...> return 3.14*a*b;
...> }
...> }
| 已替换 类 Maths
jshell> Maths.area(1);
$68 ==> 3.14
jshell> Maths.area(2,3);
$69 ==> 18.84
jshell>
6.8 包
默认情况下,JShell提供了一些常用包的导入 可以输入以下命令列出所有导入的包:
代码语言:javascript复制jshell> /imports
| import java.io.*
| import java.math.*
| import java.net.*
| import java.nio.file.*
| import java.util.*
| import java.util.concurrent.*
| import java.util.function.*
| import java.util.prefs.*
| import java.util.regex.*
| import java.util.stream.*
jshell>