文章目录
java work week-one
1.打印乘法口诀
public class OneMultiplication {
public static void main(String[] args) {
for (int i=1; i <= 9; i++) {
for (int j=1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + i*j + "\t");
}
System.out.println();
}
}
}
2.控制台输入5个数,从小到大输出
import java.util.Arrays;
import java.util.Scanner;
public class TowNeusoft {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] a = new int[5];
for (int i = 0; i < 5; i++) {
a[i] = sc.nextInt();
}
Arrays.sort(a);
for (int i : a) {
System.out.println(i);
}
}
}
或者,自己写起泡排序…
import java.util.Scanner;
public class TwoSort {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num[] = new int[5];
System.out.println("请输入5个数(空格隔开):");
for (int i=0; i < 5; i++) {
num[i] = sc.nextInt();
}
num = newSort(num);
for (int i=0; i<num.length; i++) {
System.out.print(num[i] + " ");
}
}
public static int[] newSort(int[] num) {
for (int i=0; i < num.length-1; i++) {
for (int j=0; j< num.length-i-1; j++) {
if (num[j] > num[j+1]) {
int temp = num[j];
num[j] = num[j+1];
num[j+1] = temp;
}
}
}
return num;
}
}
3.设计数学公式工具类,实现不同图形的求周长、面积方法。(多态)
abstract class Shape {
abstract float perimeter(); // 计算周长
abstract float area(); // 计算面积
}
// 长方形类
class Rectangle extends Shape {
private float length; // 长
private float width; // 宽
public Rectangle(float length, float width) {
this.length = length;
this.width = width;
}
public void setLength(float length) {
this.length = length;
}
public float getLength() {
return length;
}
public void setWidth(float width) {
this.width = width;
}
public float getWidth() {
return width;
}
@Override
float perimeter() {
// 返回-1说明所输入参数不能构成相应图形
return length>0 && width>0 ? (length + width) * 2 : -1;
}
@Override
float area() {
return length>0 && width>0 ? length*width : -1;
}
}
// 圆形类
class Circle extends Shape {
private float radius; // 半径
public Circle(float radius) {
this.radius = radius;
}
public void setRadius(float radius) {
this.radius = radius;
}
public float getRadius() {
return radius;
}
@Override
float perimeter() {
return radius>0 ? (float)Math.PI*2*radius : -1;
}
@Override
float area() {
return radius>0 ? (float)Math.PI*radius*radius : -1;
}
}
// 三角形类
class Triangle extends Shape {
private float a; // 第一条边
private float b; // 第二条边
private float c; // 第三条边
public Triangle(float a, float b, float c) {
this.a = a;
this.b = b;
this.c = c;
}
public void setA(float a) {
this.a = a;
}
public void setB(float b) {
this.b = b;
}
public void setC(float c) {
this.c = c;
}
public float getA() {
return a;
}
public float getB() {
return b;
}
public float getC() {
return c;
}
// 判断三条边是否能构成三角形
private boolean isTriangle(float a, float b, float c) {
if(a>0 && b>0 && c>0) {
if((a+b>c && Math.abs(a-b)<c) || (a+c>b && Math.abs(a-c)<b) || (b+c>a && Math.abs(b-c)<a)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
@Override
float perimeter() {
return isTriangle(a, b, c) ? a+b+c :-1;
}
@Override
float area() {
float s = (a+b+c) / 2;
return isTriangle(a, b, c) ? (float)Math.pow(s*((s-a)*(s-b)*(s-c)), 0.5) : -1;
}
}
public class ThreeShape {
public static void main(String[] args) {
Rectangle r = new Rectangle(3, 4);
Circle c = new Circle(2.5f);
Triangle t = new Triangle(2, 2, 1);
System.out.println("长方形:长" + r.getLength() + "、宽" + r.getWidth() + "、周长" + r.perimeter() + "、面积" + r.area());
System.out.println("圆形:半径" + c.getRadius() + "、周长" + c.perimeter() + "、面积" + c.area());
System.out.println("三角形:边一" + t.getA() + "、边二" + t.getB() + "、边三" + t.getC() + "、周长" + t.perimeter() + "、面积" + t.area());
}
}
4.使用* 打印空心菱形(长度随机)
import java.util.Random;
public class FourNeusoft {
public static void main(String[] args) {
Random rd = new Random();
// nextInt(5)是调用这个对象的nextInt方法生成一个0-4的随机数,length的值等于5加上一个0-4的随机数,等于5-9。
int length = rd.nextInt(5)+5;
// 打印上三角形
for (int i = 1; i <= length; i++){
for (int j = 1; j <= length-i; j++){
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++){
if (j == 1 || j == 2 * i - 1){
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
// 打印下三角形
for (int i = length - 1; i >= 1; i--){
for (int j = 1; j <= length - i; j++){
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++){
if (j == 1 || j == 2 * i - 1){
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
5.总结重写和重载规则
重写(覆盖)(override):子类对父类的允许访问的方法的实现过程进行重新编写, 返回值和形参都不能改变。子类能够根据需要实现(属于子类自己的)父类的方法。
规则:
1)当子类对象的重写方法被调用时,无论通过子类的引用调用还是通过父类的引用调用,运行的都是子类重写后的方法(父类方法被覆盖);
2)参数列表必须完全与被重写方法的相同;
3)返回值类型必须完全与被重写方法的返回值类型相同;
4)访问权限不能比父类中被重写的方法的访问权限更低;
5)声明为final的方法不能被重写;
6)声明为static的方法不能被重写(但是能够被再次声明);
7)子类和父类在同一个包中,那么子类可以重写父类所有方法,除了声明为private和final的方法。;
8)子类和父类不在同一个包中,那么子类只能够重写父类的声明为public和protected的非final方法;
9)构造方法不能被重写。
重载(overload):在一个类里面,方法名字相同,但参数不同(返回值类型可以相同或不同)。
规则:1)被重载的方法必须改变参数列表(参数个数或类型或顺序不一样);
2)被重载的方法可以改变返回值类型;
3)被重载的方法可以改变访问修饰符;
4)被重载的方法可以声明新的或更广的检查异常;
5)方法能够在同一个类中或者在一个子类中被重载。
重载与重写的区别:
1)重载是一个编译期概念;重写是一个运行期间概念。
2)重载遵循“编译期绑定”,即在编译时根据参数变量的类型判断应该调用哪个方法;重写遵循“运行期绑定”,即在运行的时候,根据引用变量所指向的实际对象的类型来调用方法。
3)因为在编译期已经确定调用哪个方法,所以重载并不是多态。而重写是多态。重载只是一种语言特性,是一种语法规则,与多态无关,与面向对象也无关。(注:严格来说,重载是编译时多态,即静态多态。但是,Java中提到的多态,在不特别说明的情况下都指动态多态)
附加题:重写类的equals方法实现比值操作,重写toString方法实现属性值输出
重写类的equals方法实现比值操作,重写toString方法实现属性值输出
java.lang.Object类的equals()方法代码:
public boolean equals(Object obj) {
return (this == obj);
}
Object类中equals方法是比较引用地址来判断两对象是否是同一个对象
重写equals方法需要遵循Java如下规则:
自反性:对于任意的对象x,x.equals(x)返回true(自己一定等于自己);
对称性:对于任意的对象x和y,若x.equals(y)为true,则y.equals(x)亦为true;
传递性:对于任意的对象x、y和z,若x.equals(y)为true且y.equals(z)也为true,则x.equals(z)亦为true;
一致性:对于任意的对象x和y,x.equals(y)的第一次调用为true,那么x.equals(y)的第二次、第三次、第n次调用也均为true,前提条件是没有修改x也没有修改y;
对于非空引用x,x.equals(null)永远返回为false。
java.lang.Object类的toString()方法代码
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
class Student {
private int id;
private String name;
private int age;
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
final Student other = (Student) obj;
if (age != other.age)
return false;
if (id != other.id)
return false;
if (name == null) {
return other.name == null;
} else return name.equals(other.name);
}
@Override
public String toString() {
return id + ":" + name + ":" + age;
}
}
这是公司老师给的代码,然而我看网上小可爱们喜欢的是…(我喜欢比较属性值那里…)
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
@Override
public boolean equals(Object obj) {
if (this == obj) { // 自反性
return true;
}
if (!(obj instanceof Person)) { // 任何对象不等于null,比较是否为同一类型
return false;
}
Person p = (Person) obj; // 强制类型转换
//比较属性值
return getName().equals(p.getName()) && getAge() == p.getAge();
}
@Override
public String toString() {
return "name:" + name + ", age:" + age;
}
}
public class MoreOverrideEqualsAndToString {
public static void main(String[] args) {
Person p1 = new Person("zhangsan", 22);
Person p2 = new Person("zhangsan", 22);
System.out.println(p1.equals(p2));
System.out.println(p1.toString());
}
}
2020,祝大家抗疫成功岁岁安!
上次写博客还是签了公司又懒得弄论文的时候,于是就找了本java书看,跟着学学敲敲…顺便发一发笔记,自己日后找起来也是方便…
很神奇,我当初准备的是NLP和前端,然后被java开发录用了,那么既来之则安之…7月毕业的我现在公司在组织培训…就是发布作业然后写作业然后传到老师邮箱,我还在家写我的毕业论文,也是期待自己能从一只java小白变成一只大白…也在这里…也不知道有没有看我的小伙伴们…一起来见证我的成长吧…