package com.atguigu.exer; /* * 定义一个ManKind类,包括 成员变量int sex和int salary; 方法void manOrWoman():根据sex的值显示“man”(sex==1)或者“woman”(sex==0); 方法void employeed():根据salary的值显示“no job”(salary==0)或者“ job”(salary!=0)。 */ public class ManKind { private int sex;//性别 private int salary;//薪资 public ManKind() { } public ManKind(int sex, int salary) { this.sex = sex; this.salary = salary; } public void manOrWoman(){ if(sex == 1){ System.out.println("man"); }else if(sex == 0){ System.out.println("woman"); } } public void employeed(){ // if(salary == 0){ // System.out.println("no job"); // }else{ // System.out.println("job"); // } //或 String jobInfo = (salary == 0)? "no job" : "job"; System.out.println(jobInfo); } public int getSex() { return sex; } public void setSex(int sex) { this.sex = sex; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } }
package com.atguigu.exer; /* * 定义类Kids继承ManKind,并包括 成员变量int yearsOld; 方法printAge()打印yearsOld的值。 */ public class Kids extends ManKind{ private int yearsOld; public Kids() { } public Kids(int yearsOld) { this.yearsOld = yearsOld; } public void printAge(){ System.out.println("I am " + yearsOld + " years old."); } public int getYearsOld() { return yearsOld; } public void setYearsOld(int yearsOld) { this.yearsOld = yearsOld; } /* * 修改练习1.2中定义的类Kids,在Kids中重新定义employeed()方法, * 覆盖父类ManKind中定义的employeed()方法, * 输出“Kids should study and no job.” */ public void employeed() { System.out.println("Kids should study and no job."); } }
package com.atguigu.exer; /* * 定义类KidsTest,在类的main方法中实例化Kids的对象someKid,用该对象访问其父类的成员变量及方法。 */ public class KidsTest { public static void main(String[] args) { Kids someKid = new Kids(12); someKid.printAge(); someKid.setSalary(0); someKid.setSex(1); someKid.employeed(); someKid.manOrWoman(); } }
package com.atguigu.exer1; public class Circle { private double radius;//半径 public Circle(){ radius = 1.0; } // public Circle(double radius){ this.radius = radius; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } //返回圆的面积 public double findArea(){ return Math.PI * radius * radius; } }
package com.atguigu.exer1; public class Cylinder extends Circle{ private double length;//高 public Cylinder(){ length = 1.0; } public double getLength() { return length; } public void setLength(double length) { this.length = length; } //返回圆柱的体积 public double findVolume(){ // return Math.PI * getRadius() * getRadius() * getLength(); return super.findArea() * getLength(); } @Override public double findArea() {//返回圆柱的表面积 return Math.PI * getRadius() * getRadius() * 2 + 2 * Math.PI * getRadius() * getLength(); } }
package com.atguigu.exer1; public class CylinderTest { public static void main(String[] args) { Cylinder cy = new Cylinder(); cy.setRadius(2.1); cy.setLength(3.4); double volume = cy.findVolume(); System.out.println("圆柱的体积为:" + volume); //没有重写findArea()时: // double area = cy.findArea(); // System.out.println("底面圆的面积:" + area); //重写findArea()以后: double area = cy.findArea(); System.out.println("圆柱的表面积:" + area); System.out.println("******************"); Cylinder cy1 = new Cylinder(); double volume1 = cy1.findVolume(); System.out.println("圆柱的体积为:" + volume1); } }
package com.atguigu.java; /* * 如何调试程序: * 1. System.out.println(). * 2. Eclipse - Debug调试 * */ public class DebugTest { public static void main(String[] args) { int i = 10; int j = 20; System.out.println("i = " + i + ", j = " + j); DebugTest test = new DebugTest(); int max = test.getMax(i, j); System.out.println("max = " + max); } private int getMax(int k, int m) { int max = 0; if (k < m) { max = k; } else { max = m; } return max; } }
package com.atguigu.java; public class DebugTest1 { public static void main(String[] args) { int[] arr = new int[] {1,2,3,4,5}; System.out.println(arr);//地址值 char[] arr1 = new char[] {‘a‘,‘b‘,‘c‘}; System.out.println(arr1);//abc } }
package com.atguigu.java1; /* * 方法的重写(override / overwrite) * * 1.重写:子类继承父类以后,可以对父类中同名同参数的方法,进行覆盖操作 * * 2.应用:重写以后,当创建子类对象以后,通过子类对象调用子父类中的同名同参数的方法时,实际执行的是子类重写父类的方法。 * * 3. 重写的规定: * 方法的声明: 权限修饰符 返回值类型 方法名(形参列表) throws 异常的类型{ * //方法体 * } * 约定俗称:子类中的叫重写的方法,父类中的叫被重写的方法 * ① 子类重写的方法的方法名和形参列表与父类被重写的方法的方法名和形参列表相同 * ② 子类重写的方法的权限修饰符不小于父类被重写的方法的权限修饰符 * >特殊情况:子类不能重写父类中声明为private权限的方法 * ③ 返回值类型: * >父类被重写的方法的返回值类型是void,则子类重写的方法的返回值类型只能是void * >父类被重写的方法的返回值类型是A类型,则子类重写的方法的返回值类型可以是A类或A类的子类 * >父类被重写的方法的返回值类型是基本数据类型(比如:double),则子类重写的方法的返回值类型必须是相同的基本数据类型(必须也是double) * ④ 子类重写的方法抛出的异常类型不大于父类被重写的方法抛出的异常类型(具体放到异常处理时候讲) * ********************************************************************** * 子类和父类中的同名同参数的方法要么都声明为非static的(考虑重写),要么都声明为static的(不是重写)。 * * #区分方法的重载与重写 */ public class PersonTest { public static void main(String[] args) { Student s = new Student("计算机科学与技术"); s.eat(); s.walk(10); System.out.println("**************"); s.study(); Person p1 = new Person(); p1.eat(); } }
package com.atguigu.java1; public class Person { String name; int age; public Person(){ } public Person(String name,int age){ this.name = name; this.age = age; } void eat(){ System.out.println("吃饭"); } public void walk(int distance){ System.out.println("走路,走的距离是:" + distance + "公里"); show(); eat(); } private void show(){ System.out.println("我是一个人"); } public Object info(){ return null; } public double info1(){ return 1.0; } }
package com.atguigu.java1; public class Student extends Person{ String major; public Student(){ } public Student(String major){ this.major = major; } public void study(){ System.out.println("学习。专业是:" + major); } //对父类中的eat()进行了重写 public void eat(){ System.out.println("学生应该多吃有营养的食物"); } public void show(){ System.out.println("我是一个学生"); } public String info(){ return null; } // public int info1(){ // return 1; // } // public void walk(int distance){ // System.out.println("重写的方法"); // } public void walk(int distance) { System.out.println("重写的方法"); } }
原文:https://www.cnblogs.com/LXL616/p/10902706.html