报纸网站建设,服装网站设计理念,wordpress电影资讯,抽奖小程序今日内容 零、 复习昨日 一、一日一题(数组,OOP) 二、面向对象练习#xff08;方法参数返回值#xff09; 三、局部变量成员变量 四、this关键字 五、构造方法 六、重载 七、封装 小破站同步上课视频: https://space.bilibili.com/402601570/channel/collectiondetail?…今日内容 零、 复习昨日 一、一日一题(数组,OOP) 二、面向对象练习方法参数返回值 三、局部变量成员变量 四、this关键字 五、构造方法 六、重载 七、封装 小破站同步上课视频: https://space.bilibili.com/402601570/channel/collectiondetail?sid1091094 零、 复习昨日 见晨考 一、一日一题
2 写一个方法 用于合并两个int类型的数组 合并法则如下合并去重排序 {1,2,5,8,9}{1,3,0}----{0,1,2,3,5,8,9}
package com.qf.homework;import java.util.Arrays;/*** --- 天道酬勤 ---** author QiuShiju* desc*/
public class Homework {public static void main(String[] args) {int[] arr1 {1,2,5,8,9};int[] arr2 {1,3,0,8};plus(arr1,arr2);}// {1,2,5,8,9}{1,3,0}----{0,1,2,3,5,8,9}// 先合并再排序后去重public static void plus(int[] arr1,int[] arr2) {// 合并int[] arr3 new int[arr1.length arr2.length];System.arraycopy(arr1,0,arr3,0,arr1.length);System.arraycopy(arr2,0,arr3,arr1.length,arr2.length);System.out.println(Arrays.toString(arr3) );// 去重// 创建新数组,存放不重复的元素int[] arr4 new int[arr3.length];Arrays.sort(arr3);arr4[0] arr3[0];int index 1;// 取原数组元素for (int i 1; i arr3.length; i) {boolean flag false;// 不重复// 取去重后数组的元素for (int j 0; j arr4.length; j) {if (arr3[i] arr4[j]) {flag true;}}if (!flag) {arr4[index] arr3[i];index;}}int[] arr5 new int[index];System.arraycopy(arr4,0,arr5,0,index);System.out.println(Arrays.toString(arr5));}
}3 面向对象
PDF5T10 根据要求创建类和对象
public class Worker {String name; // 姓名int age;//年龄double salary;// 薪水public void work(){System.out.println(工人工作 );}public void work(int hour) {System.out.println(工人工作时长:hour );}}
// 同一个java文件中只能有一个公共的类
// 且公共的类的名字要和外面文件名一样
class TestWorker{public static void main(String[] args) {Worker worker new Worker( );worker.age 19;worker.name zzz;worker.work();worker.work( 10 );}}T11
public class Address {String address;String zipCode;}T12
package com.qf.homework;/*** --- 天道酬勤 ---** author QiuShiju* desc 引用类型: 数组,类,接口*/
public class Worker {String name; // 姓名int age;//年龄double salary;// 薪水// 地址类属性Address addr;//数据类型 变量名;public void work(){System.out.println(工人工作 );}public void work(int hour) {System.out.println(工人工作时长:hour );}
}
// 同一个java文件中只能有一个公共的类
// 且公共的类的名字要和外面文件名一样
class TestWorker{public static void main(String[] args) {Worker worker new Worker( );worker.age 25;worker.name zhangsan;worker.salary 2500;// Address addr new Address();// worker.addr addr;worker.addr new Address();worker.addr.address 郑州; // 注意空指针异常问题worker.addr.zipCode 4000;System.out.println(worker.age );System.out.println(worker.salary );System.out.println(worker.name );System.out.println(worker.addr.address );System.out.println(worker.addr.zipCode );}
}二、面向对象练习
练习方法参数,返回值的使用
public class Phone {String brand;double price;public boolean call(String num) {System.out.println(brand 打电话 num);System.out.println(这个手机价格: price);return true;}}
class TestPhone {public static void main(String[] args) {Phone phone new Phone( );phone.brand MI;phone.price 1888.0;boolean isok phone.call(120);System.out.println(打电话是否成功? isok );}}三、局部变量成员变量
局部变量成员变量位置作用域初始值内存位置生命周期
public class Phone {/*** 成员属性就是成员变量* 1 类中方法外* 2 类中所有方法都可以使用* 3 成员变量会随着创建对象时被赋默认值(即可以不用主动考虑初始值的事)* 4 成员变量在内存的堆中* 5 在对象创建时出现并初始化,失去引用后,会把垃圾回收机制处理(GC)*/String brand;double price;/*** 局部变量* 1 方法参数列表内和方法体内* 2 只能在方法内部使用* 3 局部变量使用前必须主动赋值* 4 局部变量是在栈的栈帧中* 5 随着方法进栈而出现,方法执行完出栈而消失*/public boolean call(String num) {int a 1;System.out.println(a );System.out.println(brand 打电话 num);System.out.println(这个手机价格: price);return true;}
}四、this关键字 this就是代表当前对象 当前对象是指,谁调用这个方法,this就代表调用这个方法的对象 因为this代表的是对象,所以它就可以调用对象的属性和方法 this.属性; this.方法(); package com.qf.oop;/*** --- 天道酬勤 ---** author QiuShiju* desc*/
public class Car {String brand;double price;public void run() {int a 1;String brand 五菱;System.out.println(this:this );// 输出的是car的地址,代表car对象/*** 当成员变量和局部变量重名时* 使用时,默认使用方法自己的局部变量(就近原则)* 如果必须使用成员变量,就使用this来调用* 因为this代表对象,所以可以调用对象属性*/System.out.println(this.brand 车跑动,跑了a公里 );// this调用方法this.didi();}public void didi() {System.out.println(车鸣笛 );}}
class TestCar {public static void main(String[] args) {Car car new Car( );System.out.println(car: car );car.brand BYD;car.price 150000;car.run();}
}使用this的场景总结: 当成员变量和局部变量重名时,用this.属性来代表成员变量. 问
this是什么what,能干什么how,什么时候用when?
五、构造方法 构造方法又叫构造器(constructor),是用来创建对象的. 类名 对象名 new 类名(); 其实这个语法应该这么叫 类名 对象名 new 构造方法(); 特点 每个类都默认有一个隐藏的无参构造没有返回值,也不能写void方法名必须是类名只能通过new来调用 特殊的,类中没有一个显式构造方法,那么就会默认提供一个隐式无参构造 一旦显式写出任何一个构造方法,隐式的不再提供. 具体作用(构造方法一旦执行) 在堆中开启空间,分配地址值属性和方法被初始化对象创建成功,将地址值赋值给变量(对象名) 有参构造 在创建对象时,调用有参构造方法,方法内部有对属性赋值的过程.所以有参构造创建出的对象的属性是指定值.
public class Student {String name;int age;/*** 显示的写出无参构造* 构造方法语法特点* 1. 没有返回值,也不能写void* 2. 方法名必须是类名*/public Student(){System.out.println(无参构造 );}/*** 有参构造*/public Student(String name,int age) {this.name name;this.age age;}public void coding(){System.out.println(敲代码 );}
}
class TestStudent{public static void main(String[] args) {// ???? 能不能创建出的对象,内部的属性直接有值// 能!!! 有参构造Student s2 new Student(奥利安娜, 20);System.out.println(s2.age );System.out.println(s2.name);}
}总结有参构造和无参构造区别 相同点: 都能创建出对象不同点: 无参构造创建出的对象属性值都是默认值;; 有参构造创建出的对象属性值是指定值 即两者都是创建对象,只是不同的效果.看情况选择使用 六、重载 重载overload: 指方法的重载 方法是在同一个类中方法名相同,参数列表不同参数列表不同: 个数,类型,顺序不同 好处: 方便调用,根据传入的参数的个数,顺序,类型决定执行哪个方法.不用去特别的设计不同的方法名 public class TestOverload {public static void main(String[] args) {// println其实就是一系列重载的方法// 就是方便使用System.out.println( );System.out.println(1);System.out.println(1.1);System.out.println(zzz);System.out.println(new Student());System.out.println(a);m1();}public static void m1() {System.out.println(m1() );}public static void m1(int a) {System.out.println(m1(int a) );}public static void m1(String a) {System.out.println(m1(String a) );}public static void m1(String a,int b) {System.out.println(m1(String ,int b) );}public static void m1(int b,String a) {System.out.println(m1(int b,String a) );}
}Dog类,两个属性,有参无参构造,创建对象
七、类的封装 类的封装: 将代码封装在一个类中,隐藏一些代码实现的逻辑,对外提供一个公共的访问方式. 类的封装实现步骤: 属性私有提供对应的set,get方法 setter getter方法,很固定 一定是set属性首字母大写 setXxx getXxxset方法有参数无返回值get方法无参数有返回值 public class People {// 私有化,这个属性就只能在当前类中使用private int age;private String name;// 提供set方法供赋值public void setAge(int age) {if (age 120 age 0) {this.age age;}else{System.out.println(年龄有误 );}}// 提供get方法供取值public int getAge() {return age;}public void setName(String name) {this.name name;}public String getName() {return name;}public void eat(){System.out.println(人吃饭 );}}class TestPeople{public static void main(String[] args) {People people new People( );// people.name ikun;// people.age -22;people.setAge(22);int age people.getAge( );System.out.println(age);people.setName(kunkun);String name people.getName( );System.out.println(name );}
}练习: 封装上题Dog类
八、总结
局部变量成员变量 能分清初始值不同
this代表当前对象场景用来区分成员变量和局部变量
构造方法 用来创建对象有参和无参构造的区别
重载 是代码的一种设计方式可以让方法调用更方便
类的封装先属性私有后提供一对儿setget方法九、作业
重复2遍代码
作业.txt
pdf5中的每个题特别10,11,12
文章转载自: http://www.morning.qqnh.cn.gov.cn.qqnh.cn http://www.morning.kbqqn.cn.gov.cn.kbqqn.cn http://www.morning.dywgl.cn.gov.cn.dywgl.cn http://www.morning.dfmjm.cn.gov.cn.dfmjm.cn http://www.morning.dwrbn.cn.gov.cn.dwrbn.cn http://www.morning.jyzxt.cn.gov.cn.jyzxt.cn http://www.morning.byjwl.cn.gov.cn.byjwl.cn http://www.morning.fjptn.cn.gov.cn.fjptn.cn http://www.morning.hdrsr.cn.gov.cn.hdrsr.cn http://www.morning.kdnrc.cn.gov.cn.kdnrc.cn http://www.morning.htsrm.cn.gov.cn.htsrm.cn http://www.morning.yzxhk.cn.gov.cn.yzxhk.cn http://www.morning.mbhdl.cn.gov.cn.mbhdl.cn http://www.morning.yrrnx.cn.gov.cn.yrrnx.cn http://www.morning.tbhf.cn.gov.cn.tbhf.cn http://www.morning.jrtjc.cn.gov.cn.jrtjc.cn http://www.morning.rbylq.cn.gov.cn.rbylq.cn http://www.morning.ylqpp.cn.gov.cn.ylqpp.cn http://www.morning.jwgnn.cn.gov.cn.jwgnn.cn http://www.morning.mjbjq.cn.gov.cn.mjbjq.cn http://www.morning.snbry.cn.gov.cn.snbry.cn http://www.morning.mtbsd.cn.gov.cn.mtbsd.cn http://www.morning.rgdcf.cn.gov.cn.rgdcf.cn http://www.morning.pqryw.cn.gov.cn.pqryw.cn http://www.morning.tcxk.cn.gov.cn.tcxk.cn http://www.morning.mtqqx.cn.gov.cn.mtqqx.cn http://www.morning.xsfg.cn.gov.cn.xsfg.cn http://www.morning.tphrx.cn.gov.cn.tphrx.cn http://www.morning.dfhkh.cn.gov.cn.dfhkh.cn http://www.morning.njdtq.cn.gov.cn.njdtq.cn http://www.morning.27asw.cn.gov.cn.27asw.cn http://www.morning.wqtzs.cn.gov.cn.wqtzs.cn http://www.morning.rcww.cn.gov.cn.rcww.cn http://www.morning.hcsqznn.cn.gov.cn.hcsqznn.cn http://www.morning.knnhd.cn.gov.cn.knnhd.cn http://www.morning.ptmch.com.gov.cn.ptmch.com http://www.morning.bkslb.cn.gov.cn.bkslb.cn http://www.morning.hbhnh.cn.gov.cn.hbhnh.cn http://www.morning.fhtmp.cn.gov.cn.fhtmp.cn http://www.morning.tzlfc.cn.gov.cn.tzlfc.cn http://www.morning.pdwny.cn.gov.cn.pdwny.cn http://www.morning.skpdg.cn.gov.cn.skpdg.cn http://www.morning.lrybz.cn.gov.cn.lrybz.cn http://www.morning.kxqwg.cn.gov.cn.kxqwg.cn http://www.morning.llthz.cn.gov.cn.llthz.cn http://www.morning.jcwhk.cn.gov.cn.jcwhk.cn http://www.morning.zqmdn.cn.gov.cn.zqmdn.cn http://www.morning.cbvlus.cn.gov.cn.cbvlus.cn http://www.morning.zyrcf.cn.gov.cn.zyrcf.cn http://www.morning.ykwbx.cn.gov.cn.ykwbx.cn http://www.morning.fldsb.cn.gov.cn.fldsb.cn http://www.morning.gnyhc.cn.gov.cn.gnyhc.cn http://www.morning.zrpbf.cn.gov.cn.zrpbf.cn http://www.morning.wmqrn.cn.gov.cn.wmqrn.cn http://www.morning.hpspr.com.gov.cn.hpspr.com http://www.morning.pzss.cn.gov.cn.pzss.cn http://www.morning.hjbrd.cn.gov.cn.hjbrd.cn http://www.morning.prznc.cn.gov.cn.prznc.cn http://www.morning.dwhnb.cn.gov.cn.dwhnb.cn http://www.morning.spghj.cn.gov.cn.spghj.cn http://www.morning.xwzsq.cn.gov.cn.xwzsq.cn http://www.morning.cmqrg.cn.gov.cn.cmqrg.cn http://www.morning.jfjfk.cn.gov.cn.jfjfk.cn http://www.morning.kwqqs.cn.gov.cn.kwqqs.cn http://www.morning.fyglr.cn.gov.cn.fyglr.cn http://www.morning.srgsb.cn.gov.cn.srgsb.cn http://www.morning.ljglc.cn.gov.cn.ljglc.cn http://www.morning.cjwkf.cn.gov.cn.cjwkf.cn http://www.morning.skdrp.cn.gov.cn.skdrp.cn http://www.morning.jjrsk.cn.gov.cn.jjrsk.cn http://www.morning.txfzt.cn.gov.cn.txfzt.cn http://www.morning.kkhf.cn.gov.cn.kkhf.cn http://www.morning.dighk.com.gov.cn.dighk.com http://www.morning.kztpn.cn.gov.cn.kztpn.cn http://www.morning.nhdw.cn.gov.cn.nhdw.cn http://www.morning.fpjw.cn.gov.cn.fpjw.cn http://www.morning.fbmzm.cn.gov.cn.fbmzm.cn http://www.morning.gdpai.com.cn.gov.cn.gdpai.com.cn http://www.morning.rqmr.cn.gov.cn.rqmr.cn http://www.morning.qrmyd.cn.gov.cn.qrmyd.cn