package Java第四章; class Point{ //定义x,y坐标 double x,y; //定义有参构造函数 public Point(double x,double y) { x = x; y = y; } //定义方法 public double getDent(Point p) { return Math.sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y)); } } public class TestConstructor{ public static void main(String[] args) { Point p = new Point(3.0,4.0); Point num = new Point(0.0,0.0); System.out.println(p.getDent(num)); } }
为什么结果是0?
这个结果是5
import java.util.Scanner; /** * 代码示例: * @author xiaoding * */ class Point { double x, y; public Point(double _x, double _y) { x = _x; y = _y; } public double getDistance(Point p) { return Math.sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y)); } } public class Example { public static void main(String[ ] args) { Point p1 = new Point(3.0, 4.0); Point origin = new Point(0.0, 0.0); System.out.println(p1.getDistance(origin)); } }
package Java第四章; /** * @author xiaoding * 定义类和对象 */ public class TestStudent { //定义成员变量 int id; //学号 String name; //姓名 char gender; //性别 String major; //专业 String school; //毕业院校 //调用另一个类 Brand comper; //方法 void study() { System.out.println("学习"); System.out.println("吃饭"); System.out.println("写作业"); System.out.println("考试"); } //在Java中,main方法必须放在类里面,程序的执行入口 public static void main(String[] args) { //创建对象 TestStudent student = new TestStudent(); //调用类的属性并赋值 student.id = 1; student.name = "丁天赐"; student.gender = '男'; student.major = "移动app软件开发"; student.school = "南京新华电脑专修学院"; //调用方法 student.study(); Brand comp = new Brand(); comp.computer = "联想"; student.comper = comp; //打印对象属性并输出 System.out.println(student.id); System.out.println(student.name); System.out.println(student.gender); System.out.println(student.major); System.out.println(student.school); System.out.println(student.comper); } } class Brand{ String computer;//电脑 }
为什么?
明明comp.computer已经赋值了,
将comp的值赋值给comper可打印的结果却是那个类方法的名称
找到有个部分敲错了,现在图片可以显示,但是为什么出现两个?
上个问题图没传上来,运行后就是这样的
老师,这个图片加载不出来是怎么回事啊??
老师在表格思想中理解数据库存储方式,提到所属具有都是一张表格,表格里面有结构,数据,但是一张表格不能完成表示一个对象的信息,那么就需要多个表格,标签之间可以嵌套,相互之间有关联。
在面向对象中,本质上所有数据都是表结构,表结构(表头)对应的是类结构,类的功能,一行数据代表一个对象,
在面向对象中,不仅需要静态数据,还需要有动作才能组成一个完成面向对象,那么面向对象里面有方法、数据,是否也想表格思想那样,一个类里面的所有对象和方法,不能完成表示所有的数据和动作,需要关联其他类,类和类之前有联系,可以嵌套,并且类和类之前设有访问权限
//使用循环计算阶乘 int num = 5;//阶乘 int num1 = 1; while (num > 1) { //为什么这个只需要计算两次 num1 *= num * (num - 1); num -= 2; System.out.println(num); }
//使用if-else判断三个数的最大值,并返回出来 //如果使用三木运算符,我可以理解,但是使用if-else我却理解不了 public static int add(int num,int num1,int num2) { if ((num > num1) && (num > num2)) { return num; }else if ((num1 > num2) && (num1 > num)) { return num1; }else { return num2; } }
老师请问一下,equals方法在没有被重写之前,不就是比较是不是同一个对象吗,str1是一个字符串对象,str2是一个字符串常量,两者肯定不是一个对象,为啥在使用equals方法的时候就返回true了,不是应该返回false吗,不是一个对象的
老师这个出现了错误,和视频里面的不一样,视频中抽象类不能实列化
老师你看一下,我的计算个税的算法,有没有问题
//个人税计算 //全额计算:应纳税所得额=工资收入金额-各项社会保险费-起征点(5000元) //应纳税额=应纳税所得额x税率-速算扣除数 double number = 0;//计算总额 do { Scanner input = new Scanner(System.in); System.out.println("请输入月薪"); double num; if (input.hasNextDouble()) { num = input.nextDouble(); System.out.println("请输入五险一金"); double num1 = input.nextDouble(); if ((num > 0) && (num <= 36000)) { number = (num - num1 - 5000) * 0.3 - 0; }else if ((num > 36000) && (num <= 144000)) { number = (num - num1 - 5000) * 0.10 - 2520; }else if ((num > 144000) && (num <= 300000)) { number = (num - num1 - 5000) * 0.20 - 16920; }else if ((num > 30000) && (num <= 420000)) { number = (num - num1 - 5000) * 0.25 - 31920; }else if ((num > 420000) && (num <= 660000)) { number = (num - num1 - 5000) * 0.30 - 52920; }else if ((num > 660000) && (num <= 960000)) { number = (num - num1 - 5000) * 0.35 - 85920; }else if (num > 960000) { number = (num - num1 - 5000) * 0.45 - 181920; }else { System.err.println("输入的值有误,请重新输入"); continue; } System.out.println("应缴税:" + number); //是否继续计算 System.out.println("是否继续计算,输入next或其他字符继续,exit退出"); String flag = input.next(); if (flag.equals("next")) { continue; }else if (flag.equals("exit")) { System.out.println("退出系统"); break; } }else { String str = input.next(); System.err.println("请输入正确数字!!!"); continue; } }while(true);
“==”两边的操作数有没有什么要求,数据类型要不要相同?
这里为啥要强制转换Person1类型?这里的obj已经通过:obj instanceof Person1 这个条件了,说明obj已经是Person1这个类创建的对象了,那么obj里面就有id这个属性了,所以为啥不能写成这样:
public boolean equals(Object obj){ if(obj==null){ //当obj==null时代表没有id。 return false; }else if(obj instanceof Person1){ if(obj.id==this.id){ return true; } } return false; }
为什么吧num1++,放前面,会导致他打印出来的数,不整齐
老师,这个地方有必要做笔记吗?还是只记部分
非常抱歉给您带来不好的体验!为了更深入的了解您的学习情况以及遇到的问题,您可以直接拨打投诉热线:
我们将在第一时间处理好您的问题!
关于
课程分类
百战程序员微信公众号
百战程序员微信小程序
©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园网站维护:百战汇智(北京)科技有限公司 京公网安备 11011402011233号 京ICP备18060230号-3 营业执照 经营许可证:京B2-20212637