toString&&equals方法
toString&&equals方法
先来看看这个题该怎样做?
分析:
1.java里的三大特性,有封装,继承,多态(方法的重载),super,this等关键字
2.常用的方法,equals方法,toString方法
3. double向字符串类型转换。
代码:
import java.util.*;
class GeometricObject
{
protected String color ;
protected double weight;
protected GeometricObject() {
color="red";
weight=1.0;
}
protected 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;
}
}
class Circle1 extends GeometricObject
{
private double radius;
public Circle1() {
super("res",1.0);
radius=1.0;
}
public Circle1(double radius) {
super("res",1.0);
this.radius = radius;
}
public Circle1(String color, double weight,double radius) {
super(color, weight);
this.radius=radius;
}
//getter setter字段
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
//计算圆的面积
public double findArea()
{
return Math.PI*radius*radius;
}
//布尔方法
public boolean equals(Circle1 c)
{
if(c.radius==this.radius)
return true;
else
return false;
}
public String toString()
{
System.out.println("圆的半径为:"+radius);
return String.valueOf(radius);
}
}
public class TestCicle {
public static void main(String[] args)
{
Circle1 c1=new Circle1("red",1.0,2.0);
Circle1 c2=new Circle1("red",1.0,2.0);
if(c1.color==c2.color)
{
System.out.println("颜色相同");
}
else
{
System.out.println("颜色不相同");
}
if(c1.equals(c1)==c2.equals(c2))
{
System.out.println("半径相同");
}
else
{
System.out.println("半径不相同");
}
System.out.println(c1.equals(c2));
c1.toString();
}
}
运行结果:
颜色相同
半径相同
true
圆的半径为:2.0
其中equals方法时用来比较当前对象的类容是否与参数指点的字符串的内容相同。
本题中比较c1和c2两个对象的半径是否相同。
String s1=new String("hello");
String s2=new String("hello");
s1.equals(s2);
结果是: true ,s1和s2的内容都是hello
注意:比较字符串是不能用"==" ,当用"==",实际是判断两个字符串是否为同一个对象,即使类容相同,但它们是不同的对象
s1==s2 ,这样比较是错误的。
需要说明的是本题中
public String toString()
{
System.out.println("圆的半径为:"+radius);
return String.valueOf(radius);
}
定义的是一个字符串方法,则返回值必须String类型的。其中radius是double类型的,需要将double类型的radius转换为String类型 ,方法有多种 String.valueOf(radius);这是其中的一种方法
还可以有其他的方法。
希望这篇小小的博客对大家有帮助,如果帖子中有错误之处还希望大家批评,指点。
toString&&equals方法的更多相关文章
- JAVA基础--toString, equals方法
==比较的是地址 equals比较的是内容. 所以要重写object的equals方法. public class TestEquals { public static void main(Strin ...
- java数组、java.lang.String、java.util.Arrays、java.lang.Object的toString()方法和equals()方法详解
public class Test { public static void main(String[] args) { int[] a = {1, 2, 4, 6}; int[] b = a; in ...
- Object类—复写equals方法,hashCode方法,toString方法
Object:所有类的根类. Object是不断抽取而来,具备着所有对象都具备的共性内容. class Person extends Object { private int age; Person( ...
- Object、String、数组的 toString() 方法和 equals() 方法及java.util.Arrays
public class Test { public static void main(String[] args) { int[] a = {1, 2, 4, 6}; int[] b = a; in ...
- Java中的Object类的toString()方法,equals()方法
Object类是所有类的父类,若没有明确使用extends关键字明确表示该类继承哪个类,那么它就默认继承Object类,也就可以使用Object中的方法: 1.toString 如果输出一个对象的时候 ...
- 重新编写equals()方法,hashCode()方法,以及toString(),提供自定义的相等标准,以及自描述方法
下面给出一个实例,重新编写equals()方法,提供自定义的相等标准 public class PersonTest { public static void main(String[] args) ...
- [18/11/30] toString()方法 和 equals() 方法
一. toString() 方法 Object类中定义有public String toString()方法,其返回值是 String 类型 默认: return getClass().getNam ...
- 1.9(java学习笔记)object类及toString()与equals()方法
object类 java中objec是所有类公共的父类,一个类只要没有明显的继承某一类,那么它就是继承object类. 例如 class Person {......};和class Person e ...
- 03_10_Object类的toString equals等方法
03_10_Object类的toString equals等方法 1. toString方法 Object类中定义有public String toString()方法,其返回值是String类型,描 ...
随机推荐
- 设计模式之 Observer Pattern 观察者模式
1.Subject通过一个容器保存零到多个Observer. 2.Subject通过Add,Delete方法调整Observer. 3.Subject的notifyObservers方法实际是逐个调用 ...
- 《Velocity 模板使用指南》中文版[转]
转自:http://blog.csdn.net/javafound/archive/2007/05/14/1607931.aspx <Velocity 模板使用指南>中文版 源文见 htt ...
- html引入外部的jswenjian
首先看一下引入的方式是否正确 <script type='text/javascript' src='xx.js'></script> 其次看引入的路径是否正确 如果引入了第三 ...
- java.lang.UnsupportedClassVersionError(Unsupported major.minor version 49.0)报错
报错截图如下:
- [Linux]Vim的安装及使用
1.安装:$sudo apt-get install vim 2.查看Vim所在路径$whereis vim 3.启动Vim $'/usr/bin/vim.tiny' 4. 退出Vim窗口:Ctrl ...
- Object之魔术函数__call() 处理错误调用
在提到__call之前,先来看一个实例的测试结果,以便更好地去了解__call方法的作用.上代码: <?php class Person{ function say(){ echo " ...
- coder
#include <iostream>#include <GL/glut.h>using std::cout;using std::endl;float windowWidth ...
- 你一定要知道的关于Linux文件目录操作的12个常用命令
写在前面: 1,<你一定要知道的关于Linux文件目录操作的12个常用命令>是楼主收集的关于Linux文件目录操作最常用的命令,包括文件或目录的新建.拷贝.移动.删除.查看等,是开发人员操 ...
- 【UVA1379】Pitcher Rotation (贪心+DP)
题意: 你经营者一直棒球队.在接下来的g+10天中有g(3<=g<=200)场比赛,其中每天最多一场比赛.你已经分析出你的n(5<=n<=100)个投手中每个人对阵所有m个对手 ...
- astyle代码格式化
Artistic Style 1.24 A Free, Fast and Small Automatic Formatterfor C, C++, C#, and Java Source Code 项 ...