public class Student {
//属性:姓名 年龄 性别
//新增:老师的姓名
private String name;
private int age;
private String gender;
public static String teacherName;
public Student() {
}
public Student(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
//行为
public void study() {
System.out.println(name + "正在学习");
}
public void show() {
System.out.println(name + ", " + age + ", " + gender + ", " + teacherName);
}
}
import java.util.Random;
public class StudentTest {
public static void main(String[] args) {
Student.teacherName = "阿玮老师";
//1.创建第一个学生对象
Student s1 = new Student();
s1.setName("张三");
s1.setAge(23);
s1.setGender("男");
//s1.teacherName = "阿玮老师";
s1.study();
s1.show();
//2.创建第二个学生对象
Student s2 = new Student();
s2.setName("李四");
s2.setAge(24);
s2.setGender("女");
//s2.teacherName = "阿玮老师";
s2.study();
s2.show();
}
}
public class ArrayUtil {
//私有化构造方法
//目的:为了不让外界创建他的对象
private ArrayUtil() {
}
//需要定义为静态的,方便调用
public static String printArr(int[] arr) {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < arr.length; i++) {
//i 索引 arr[i] 元素
if (i == arr.length - 1) {
sb.append(arr[i]);
} else {
sb.append(arr[i]).append(", ");
}
}
sb.append("]");
return sb.toString();
}
public static double getAverage(double[] arr) {
double sum = 0;
for (int i = 0; i < arr.length; i++) {
sum = sum + arr[i];
}
return sum / arr.length;
}
}
public class TestDemo {
public static void main(String[] args) {
//测试工具类中的两个方法是否正确
int[] arr1 = {1, 2, 3, 4, 5};
String str = ArrayUtil.printArr(arr1);
System.out.println(str);
double[] arr2 = {1.5, 3.7, 4.9, 5.8, 6.6};
double avg = ArrayUtil.getAverage(arr2);
System.out.println(avg);
}
}
public class Student {
private String name;
private int age;
private String gender;
public Student() {
}
public Student(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
import java.util.ArrayList;
public class StudentUtil {
private StudentUtil(){}
//静态方法
public static int getMaxAgeStudent(ArrayList list){
//1.定义一个参照物
int max = list.get(0).getAge();
//2.循环遍历集合
for (int i = 1; i < list.size(); i++) {
//i 索引 list.get(i)元素/学生对象 我们还需要getAge获取到年龄之后再进行比较
int tempAge = list.get(i).getAge();
if(tempAge > max){
max = tempAge;
}
}
//3.直接返回max
return max;
}
}
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
//1.创建一个集合用来存储学生对象
ArrayList list = new ArrayList<>();
//2.创建3个学生对象
Student stu1 = new Student("zhangsan",23,"男");
Student stu2 = new Student("lisi",24,"女");
Student stu3 = new Student("wangwu",25,"男");
//3把学生对象添加到集合当中
list.add(stu1);
list.add(stu2);
list.add(stu3);
//4.调用工具类中的方法
int maxAgeStudent = StudentUtil.getMaxAgeStudent(list);
System.out.println(maxAgeStudent);
}
}
public class Animal {
//权限修饰符:
//private:子类就无法访问了
//私有:只能在本类中访问
//爸爸的私房钱(自己能用)
//注意事项:
//子类只能访问父类中非私有的成员
public void eat(){
System.out.println("吃东西");
}
public void drink(){
System.out.println("喝水");
}
}
public class Cat extends Animal {
public void catchMouse() {
System.out.println("猫在抓老鼠");
}
}
public class Dog extends Animal{
public void lookHome(){
System.out.println("狗看家");
}
}
public class Husky extends Dog{
public void breakHome(){
System.out.println("哈士奇在拆家");
}
}
public class LiHua extends Cat{
}
public class Ragdoll extends Cat{
}
public class Teddy extends Dog{
public void touch(){
System.out.println("泰迪又在蹭我的腿了~");
}
}
public class Test {
public static void main(String[] args) {
//创建对象并调用方法
//1.创建布偶猫的对象
Ragdoll rd = new Ragdoll();
rd.eat();
rd.drink();
rd.catchMouse();
System.out.println("----------------------");
//2.创建哈士奇的对象
Husky h = new Husky();
h.eat();
h.drink();
h.lookHome();
h.breakHome();
}
}
public class Test {
public static void main(String[] args) {
//利用空参构造创建子类对象
Zi z1 = new Zi();
//利用带参构造创建子类对象
//Zi z2 = new Zi("zhangsan",23);
}
}
class Fu{
String name;
int age;
public Fu(){}
public Fu(String name,int age){
this.name = name;
this.age = age;
}
}
class Zi extends Fu{
//如果一个类中没有构造方法,虚拟机会自动的给你添加一个默认的空参构造
}
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException {
/* Zi z = new Zi();
z.fuShow1();*/
//把对象的地址值z以16进制的形式打在控制台上
//System.out.println(Long.toHexString(VM.current().addressOf(z)));
//因为内存分析工具需要程序不停止
// Scanner sc = new Scanner(System.in);
//sc.next();
}
}
class Fu {
private int a = 0x111;
int b = 0x222;
public void fuShow1() {
System.out.println("public --- FuShow");
}
private void fuShow2() {
System.out.println("private --- FuShow");
}
}
class Zi extends Fu {
int c = 0x333;
public void ziShow() {
System.out.println("public --- ZiShow");
}
}
public class Test {
public static void main(String[] args) {
Zi z = new Zi();
z.ziShow();
}
}
class Ye {
String name = "Ye";
}
class Fu extends Ye{
String name = "Fu";
}
class Zi extends Fu{
String name = "Zi";
public void ziShow(){
String name = "ziShow";
System.out.println(name);//ziShow
System.out.println(this.name);//Zi
System.out.println(super.name);//Fu
}
}
public class Test {
public static void main(String[] args) {
Zi z = new Zi();
z.show();
}
}
class Fu {
String name = "Fu";
String hobby = "喝茶";
}
class Zi extends Fu {
String name = "Zi";
String game = "吃鸡";
public void show() {
//如何打印Zi
//System.out.println(name);//Zi
//System.out.println(this.name);//Zi
//如何打印Fu
//System.out.println(super.name);//Fu
//如何打印喝茶
//System.out.println(hobby);//喝茶
//System.out.println(this.hobby);//喝茶
//System.out.println(super.hobby);//喝茶
//如何打印吃鸡
System.out.println(game);
System.out.println(this.game);
}
}
public class Animal {
}
public class Cat extends Animal{
}
public class Dog extends Animal{
}
public class Test {
public static void main(String[] args) {
OverseasStudent s = new OverseasStudent();
s.lunch();
}
}
class Person {
public void eat() {
System.out.println("吃米饭,吃菜");
}
public void drink() {
System.out.println("喝开水");
}
}
//留学生
class OverseasStudent extends Person{
public void lunch(){
this.eat();//吃意大利面
this.drink();//喝凉水
super.eat();//吃米饭,吃菜
super.drink();//喝开水
}
//应用场景:当父类中方法,不能满足子类现在的需求时,我们就需要把这个方法进行重写。
//注意:子类中重写的方法上面需要加上@override
@Override
public void eat() {
System.out.println("吃意大利面");
}
@Override
public void drink() {
System.out.println("喝凉水");
}
}
class Student extends Person{
public void lunch(){
//先在本类中查看eat和drink方法,就会调用子类的,如果没有,就会调用从父类中继承下来的eat和drink方法
this.eat();
this.drink();
//直接调用父类中的eat和drink方法
super.eat();
super.drink();
}
}
public class ChineseDog extends Dog{
//父类中的方法不能满足我们的需求了,所以需要进行重写
//而且中华田园犬完全用不到父类中的代码的,所以不需要通过super进行调用
@Override
public void eat() {
System.out.println("吃剩饭");
}
}
public class Dog {
public void eat() {
System.out.println("狗在吃狗粮");
}
public void drink() {
System.out.println("狗在喝水");
}
public void lookHome() {
System.out.println("狗在看家");
}
}
public class DogTest {
public static void main(String[] args) {
//创建对象并调用
Husky h = new Husky();
h.eat();
h.drink();
h.lookHome();
h.breakHome();
ChineseDog cd = new ChineseDog();
cd.eat();
cd.drink();
cd.lookHome();
}
}
public class Husky extends Dog{
//哈士奇有一个额外的方法拆家
public void breakHome(){
System.out.println("哈士奇又在拆家了");
}
}
public class SharPei extends Dog{
//因为沙皮狗吃的狗粮和骨头
//父类中的方法不能满足我们的需求了,所以需要进行重写
@Override
public void eat() {
super.eat();//吃狗粮
System.out.println("狗啃骨头");
}
}
public class Person {
String name;
int age;
public Person() {
System.out.println("父类的无参构造");
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
public class Student extends Person{
public Student(){
//子类构造方法中隐藏的super()去访问父类的无参构造
super();
System.out.println("子类的无参构造");
}
public Student(String name,int age){
super(name,age);
}
}
public class Test {
public static void main(String[] args) {
//创建学生对象
Student s = new Student("zhangsan",23);
}
}
public class Student {
String name;
int age;
String school;
//需求:
//默认为传智大学
public Student() {
//表示调用本类其他构造方法
//细节:虚拟机就不会再添加super();
this(null,0,"传智大学");
}
public Student(String name, int age, String school) {
this.name = name;
this.age = age;
this.school = school;
}
}
public class Test {
public static void main(String[] args) {
Student s = new Student();
}
}
public class Cook extends Employee{
public Cook() {
}
public Cook(String id, String name, double salary) {
super(id, name, salary);
}
@Override
public void work() {
System.out.println("厨师正在炒菜");
}
}
public class Employee {
//1.类名见名知意
//2.所有的成员变量都需要私有
//3.构造方法(空参 带全部参数的构造)
//4.get/set
private String id;
private String name;
private double salary;
public Employee() {
}
public Employee(String id, String name, double salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
//工作
public void work(){
System.out.println("员工在工作");
}
//吃饭
public void eat(){
System.out.println("吃米饭");
}
}
public class Manager extends Employee{
private double bouns;
//空参构造
public Manager() {
}
//带全部参数的构造
//父类 + 子类
public Manager(String id, String name, double salary, double bouns) {
super(id, name, salary);
this.bouns = bouns;
}
public double getBouns() {
return bouns;
}
public void setBouns(double bouns) {
this.bouns = bouns;
}
@Override
public void work() {
System.out.println("管理其他人");
}
}
public class Test {
public static void main(String[] args) {
//创建对象并赋值调用
Manager m = new Manager("heima001","张三",15000,8000);
System.out.println(m.getId() + ", " + m.getName() +
", " + m.getSalary() + ", " + m.getBouns());
m.work();
m.eat();
Cook c = new Cook();
c.setId("heima002");
c.setName("李四");
c.setSalary(8000);
System.out.println(c.getId() + ", " + c.getName() + ", " + c.getSalary());
c.work();
c.eat();
}
}