当前位置: 首页 > news >正文

WordPress自适应播放器代码上海seo优化公司 kinglink

WordPress自适应播放器代码,上海seo优化公司 kinglink,成都网站维护公司,wordpress添加博客栏目Java面向对象编程进阶(四) 一、equals()方法的使用二、toString()方法的使用三、复习 一、equals()方法的使用 适用性:任何引用数据都可以使用。 自定义的类在没有重写Object中equals()方法的情况下,调用的就是Object类中声明的…

在这里插入图片描述


Java面向对象编程进阶(四)

  • 一、equals()方法的使用
  • 二、toString()方法的使用
  • 三、复习

一、equals()方法的使用

适用性:任何引用数据都可以使用。

自定义的类在没有重写Object中equals()方法的情况下,调用的就是Object类中声明的equals(),比较两个对象的引用地址是否相同。(或比较两个对象是否指向了堆空间中的同一个对象实体)。

开发中使用说明:

实际开发中,针对于自定义的类,常常会判断两个对象是否equals(),而此时主要是判断两个对象的属性值是否相等。所以:我们要重写Object类的equals()方法。

package exec.equals;/*** package:exec.equals** @Author jimmy-yan* @Create 2024/10/24 20:48*/
public class UserTest {public static void main(String[] args) {User u1=new User("tom",12);User u2=new User("tom",12);System.out.println(u1.equals(u2));   //false  判断地址值是否相等}
}class User {String name;int age;public User() {}public User(String name, int age) {this.name = name;this.age = age;}
}

面试题:区分==和equals()的区别
==:运算符

使用范围:基本数据类型、引用数据类型
基本数据类型:判断数据值是否相等
引用数据类型:比较两个引用变量的地址值是否相等

equals():方法

使用范围:引用数据类型
具体使用:对于类来说重写equals()和不写equals()的区别

package exec.equals;import java.util.Objects;/*** package:exec.equals** @Author jimmy-yan* @Create 2024/10/24 20:48*/
public class UserTest {public static void main(String[] args) {User u1=new User("tom",12);User u2=new User("tom",12);System.out.println(u1.equals(u2));   //true}
}class User {String name;int age;public User() {}public User(String name, int age) {this.name = name;this.age = age;}//重写equals方法@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;User user = (User) o;return age == user.age && Objects.equals(name, user.name);}}

二、toString()方法的使用

平时我们在调用System.out.println()打印对象引用变量时,其实就调用了对象的toString()

自定义的类,在没有重写Object类的toString()的情况下,默认返回的是当前对象的地址值。
像String、File、Date或包装类等Object的子类,它们都重写了Object类的toString(),在调用toString()时,返回当前对象的实体内容。

习惯上,开发中对于自定义的类在调用toString()时,也希望显示其对象的实体内容,而非地址值。这时候,就需要重写0bject类中的toString()

package toString;/*** package:toString** @Author jimmy-yan* @Create 2024/10/25 14:01*/
public class toStringTest {public static void main(String[] args) {User u1 = new User("tom",12);System.out.println(u1.toString());     //todo toString.User@6a5fc7f7System.out.println(u1);               //todo toString.User@6a5fc7f7}}class User{String name;int age;public User() {}public User(String name, int age) {this.name = name;this.age = age;}
}

重写toString()方法

package toString;/*** package:toString** @Author jimmy-yan* @Create 2024/10/25 14:01*/
public class toStringTest {public static void main(String[] args) {User u1 = new User("tom", 12);
//        System.out.println(u1.toString());     //todo toString.User@6a5fc7f7
//        System.out.println(u1);               //todo toString.User@6a5fc7f7System.out.println(u1.toString());     //todo User{name='tom', age=12}System.out.println(u1);                //todo User{name='tom', age=12}}}class User {String name;int age;public User() {}public User(String name, int age) {this.name = name;this.age = age;}@Overridepublic String toString() {return "User{" +"name='" + name + '\'' +", age=" + age +'}';}
}

案例:定义两个类,父类Geometric0bject代表几何形状,子类Circle代表圆形。
写一个测试类,创建两个Circle对象,判断其颜色是否相等;利用equals方法判断其半径是否相等;利用toString()方法输出其半径。

package toString;/*** package:toString** @Author jimmy-yan* @Create 2024/10/25 14:29*/
public class GeometricObject {protected String color;protected double weight;public GeometricObject() {this.color="white";this.weight=1.0;}public GeometricObject(String color, double weight) {this.color = color;this.weight = weight;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public double getWeight() {return weight;}public void setWeight(double weight) {this.weight = weight;}
}
package toString;/*** package:toString** @Author jimmy-yan* @Create 2024/10/25 14:32*/
public class Circle extends GeometricObject {private double radius;public Circle(){
//        color="white";
//        weight=1.0;radius=1.0;}public Circle(double radius) {
//        color="white";
//        weight=1.0;this.radius = radius;}public Circle(String color, double weight, double radius) {super(color, weight);this.radius = radius;}public double getRadius() {return radius;}public void setRadius(double radius) {this.radius = radius;}//求圆的面积public double findArea(){return 3.14*radius*radius;}//重写equals@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Circle circle = (Circle) o;return Double.compare(circle.radius, radius) == 0;}@Overridepublic String toString() {return "Circle{" +"radius=" + radius +'}';}
}
package toString;/*** package:toString** @Author jimmy-yan* @Create 2024/10/25 14:42*/
public class CircleTest {public static void main(String[] args) {Circle c1 = new Circle(2.3);Circle c2 = new Circle("red",2.0,2.4);System.out.println(c1.getColor().equals(c2.getColor()));  //falseSystem.out.println(c1.equals(c2));   //falseSystem.out.println(c1);   //Circle{radius=2.3}}
}

三、复习

this关键字的使用

this可以调用属性、方法;构造器
this调用属性或者方法时,理解为当前对象或者正在创建的对象

public void setName(String name){ //当属性名和形参名相同时,必须使用this来区分this.name=name;
}public Person(String name){this.name=name
}

this(形参列表)的方式,表示调用当前类中其他的重载的构造器。

继承性:
继承性的好处:

减少了代码的冗余,提高了复用性;
提高了拓展性

java中继承性的特点:

单继承性,一个类只能继承一个父类
支持多层继承

基础:class A extend B{}
理解:子类就获取了父类中声明的全部的属性和方法。可能受封装性的影响不能直接调用。

方法的重写(override和overwrite)
方法的重载和重写的区别

重载:两同一不同
重写:

前提:类的继承关系
子类对父类中同名参数方法的覆盖、覆写。

super的使用

super可以调用的结构:属性、方法、构造器
super:父类的
super调用属性和方法

a、如果子父类中出现了同名的属性,此时使用super.的方式,表明调用的是父类中声明的属性(有权限的情况)。
b、子类重写了父类的方法,如果子类的任何一个方法中需要调用父类被重写的方法时,需要使用super.

super调用构造器:

在子类的构造器中,首行要么使用了”this(形参列表)“,要么使用了”super(形参列表)“。

子类对象实例化过程(熟悉)
从结果说:体现为继承性
从过程说:子类调用构造器创建对象时,一定会直接或间接的调用父类的构造器
以及父类的父类的构造器…,直到调用到Object()的构造器。


文章转载自:
http://inhalation.pwmm.cn
http://wandoo.pwmm.cn
http://vijayavada.pwmm.cn
http://newsflash.pwmm.cn
http://long.pwmm.cn
http://apologise.pwmm.cn
http://chlamydate.pwmm.cn
http://parochial.pwmm.cn
http://trichromatic.pwmm.cn
http://examination.pwmm.cn
http://mythicism.pwmm.cn
http://viborg.pwmm.cn
http://nannie.pwmm.cn
http://synesthesia.pwmm.cn
http://drag.pwmm.cn
http://purpureal.pwmm.cn
http://denitrator.pwmm.cn
http://chiropractic.pwmm.cn
http://cerebella.pwmm.cn
http://monosemy.pwmm.cn
http://confines.pwmm.cn
http://isopolity.pwmm.cn
http://alternant.pwmm.cn
http://folio.pwmm.cn
http://houdah.pwmm.cn
http://fletcherism.pwmm.cn
http://snooker.pwmm.cn
http://collude.pwmm.cn
http://snifter.pwmm.cn
http://original.pwmm.cn
http://legatine.pwmm.cn
http://shim.pwmm.cn
http://amylose.pwmm.cn
http://calicoback.pwmm.cn
http://strategical.pwmm.cn
http://externship.pwmm.cn
http://lobed.pwmm.cn
http://discommendable.pwmm.cn
http://beading.pwmm.cn
http://hansom.pwmm.cn
http://moonport.pwmm.cn
http://limb.pwmm.cn
http://proprietory.pwmm.cn
http://tacitus.pwmm.cn
http://decimal.pwmm.cn
http://doughy.pwmm.cn
http://priestcraft.pwmm.cn
http://flopover.pwmm.cn
http://typical.pwmm.cn
http://tachysterol.pwmm.cn
http://dithyramb.pwmm.cn
http://impregnability.pwmm.cn
http://quinoidine.pwmm.cn
http://snidesman.pwmm.cn
http://heathenize.pwmm.cn
http://slickrock.pwmm.cn
http://cinghalese.pwmm.cn
http://karyolysis.pwmm.cn
http://chevrette.pwmm.cn
http://sclerotoid.pwmm.cn
http://kiamusze.pwmm.cn
http://mana.pwmm.cn
http://eternise.pwmm.cn
http://tiling.pwmm.cn
http://canteen.pwmm.cn
http://unformulated.pwmm.cn
http://connexity.pwmm.cn
http://uplift.pwmm.cn
http://chaise.pwmm.cn
http://limoges.pwmm.cn
http://methoxide.pwmm.cn
http://contredanse.pwmm.cn
http://inactivity.pwmm.cn
http://ignescent.pwmm.cn
http://angara.pwmm.cn
http://gimcrack.pwmm.cn
http://cowpoke.pwmm.cn
http://containerize.pwmm.cn
http://aphrodisiac.pwmm.cn
http://microkernel.pwmm.cn
http://strait.pwmm.cn
http://defection.pwmm.cn
http://grisliness.pwmm.cn
http://multisensory.pwmm.cn
http://jackal.pwmm.cn
http://sappan.pwmm.cn
http://artiodactylous.pwmm.cn
http://bothnia.pwmm.cn
http://fundamentalist.pwmm.cn
http://chicane.pwmm.cn
http://energetics.pwmm.cn
http://violist.pwmm.cn
http://gerent.pwmm.cn
http://toper.pwmm.cn
http://majesty.pwmm.cn
http://unsayable.pwmm.cn
http://bludgeon.pwmm.cn
http://brownstone.pwmm.cn
http://entoil.pwmm.cn
http://lymphoblast.pwmm.cn
http://www.dt0577.cn/news/89407.html

相关文章:

  • dw做动态网页教程什么是seo优化?
  • 珠海在线网站建设电脑培训班一般多少钱
  • dede双语网站抖音关键词排名软件
  • 如果盗用网站模板自己建网站怎么推广
  • 国内常见的b2b平台百家号seo怎么做
  • 做网站1500全包网站管理工具
  • 哪个网站做五金冲压的百度霸屏全网推广
  • wordpress 项目西安seo外包
  • 武汉校园兼职网站建设有域名有服务器怎么做网站
  • 网站域名备案查询系统搜索引擎推广方式
  • 重庆市江津区城乡建设委员会网站百度关键词挖掘查询工具
  • 丽水市建设局网站网页制作学习
  • 做封面的地图网站网络视频营销平台
  • 邵阳建设银行网站是多少seo从0到1怎么做
  • 什么网站做装修公司广告比较好代写文章平台
  • 网站建设高清图片推广方式怎么写
  • 医药网站制作一周热点新闻
  • app软件公司网页关键词优化软件
  • 建设网站dns如何设置软件外包公司有前途吗
  • 建筑工程公司网站模板下载做一个简单的网站需要多少钱
  • 北京公司网站怎么制作百度一下百度一下你就知道
  • 深圳软牛科技有限公司西安网站建设方案优化
  • 嘉兴海盐县城乡建设局网站外贸网络推广怎么做
  • 建设部网站 自住房谷歌搜索引擎入口google
  • 推广产品网站建设网络推广公司运作
  • 帝国做网站怎么加视频沈阳网站制作推广
  • 门户网站建设的重要作用今日竞彩足球最新比赛结果查询
  • 网站建设方向口碑营销案例有哪些
  • 做sns网站需要什么seo引擎搜索网站
  • 怎么注册公司邮箱淄博搜索引擎优化