interface Pet {public String getName();public String getColor();public int getAge();}class Cat implements Pet {private String name;private String color;private int age;public Cat(String name, String color, int age) {this.setName(name);this.setColor(color);this.setAge(age);}public String getName(){return this.name;}public String getColor() {return this.color;}public int getAge() {return this.age;}public void setName(String name) {this.name = name;}public void setColor(String color) {this.color = color;}public void setAge(int age) {this.age = age;}}class Dog implements Pet {private String name;private String color;private int age;public Dog(String name, String color, int age) {this.name = name;this.color = color;this.age = age;}public String getName() {return this.name;}public String getColor() {return this.color;}public int getAge() {return this.age;}public void setName(String name) {this.name = name;}public void setColor(String color) {this.color = color;}public void setAge(int age) {this.age = age;}}class PetShop {private Pet[] pets;private int foot;public PetShop(int len){if (len > 0) {this.pets = new Pet[len];}else{this.pets = new Pet[1];}}public boolean add(Pet pet) {if (this.foot < this.pets.length) {this.pets[this.foot] = pet;this.foot++;return true;}else{return false;}}public Pet[] search(String keyWord){Pet p[] = null;int count = 0;for (int i = 0; i < this.pets.length; i++) {if (this.pets[i] != null) {if (this.pets[i].getName().indexOf(keyWord) != -1 || this.pets[i].getColor().indexOf(keyWord) != -1) {count++;}}}for (int i = 0; i < this.pets.length; i++) {if (this.pets[i] != null) {if (this.pets[i].getName().indexOf(keyWord) != -1) {count++;}}}p = new Pet[count];int f = 0;for (int i = 0; i < this.pets.length; i++) {if (this.pets[i] != null){if (this.pets[i].getName().indexOf(keyWord) != -1 || this.pets[i].getColor().indexOf(keyWord) != -1) {p[f] = this.pets[i];f++;}}}return p;}}public class Mini {public static void main(String args[]) {PetShop ps = new PetShop(5);ps.add(new Cat("白猫", "白色的", 2));ps.add(new Cat("黑猫", "黑色的", 3));ps.add(new Cat("花猫", "花色的", 3));ps.add(new Dog("拉布拉多", "黄色的", 3));ps.add(new Dog("金毛", "金色的", 3));ps.add(new Dog("黄狗", "黑色的", 3));print(ps.search("黑"));}public static void print(Pet p[]) {for (int i = 0; i < p.length; i++) {if (p[i] != null) {System.out.println(p[i].getName() + "," + p[i].getColor() + "," + p[i].getAge());}}}}
public class Min {public static void main(String args[]) {{int x = 30;System.out.println("普通代码块" + x);}int x = 100;System.out.println("代码块之外" + x);}}
class Person{String name;int age;static String country = "A";public Person(String name, int age){this.name = name;this.age = age;}public void info(){System.out.println(" 姓名:" + this.name + " 年龄:" + this.age + " 城市:" + country);}}public class Min{public static void main(String args[]){Person per1 = new Person("张三", 30);Person per2 = new Person("李武", 18);Person per3 = new Person("王二", 12);per1.country = "B城";per1.info();per2.info();per3.info();}}
class Person{String name;//属性int age;public void tell(){//方法System.out.println("姓名:" + name + ", 年龄:" + age);}}public class Main{public static void main(String args[]) {//主方法Person per = new Person(); //创建对象,必须实例化per.name = "张三"; //属性赋值per.age = 30;per.tell(); //调用方法}}
class Person{private String name;private int age;public void tell(){System.out.println("姓名:" + this.getName() + ", 年龄:" + this.getAge());}public String getName(){return name;}public void setName(String n){name = n;}public int getAge(){return age;}public void setAge(int a){if(a >= 0 && a < 150){age = a;}}}public class Main{public static void main(String args[]) {Person per = new Person();per.setName("张三");per.setAge(-30);per.tell();}}//封装性
栈内存.docx
1.面向对象的三大特征:封装、继承、多态。
2.类与对象的关系:类是对象的模板,对象是类的实例,类只能通过对象才可以使用。
3.类是由属性和方法组成。
4.对象的产生格式:类名称 对象名称 = new 类名称()。
5.如果一个个对象没有被实例化而直接使用,则使用时会出现空指向异常。
6.类属于引用数据类型,进行引用传递时,传递的是堆空间的使用权。
7.类的封装性:通过private关键字进行修饰,被封装的属性不能被外部直接调用,而只能通过setter或getter方法完成。只要是属性,则必须全部被封装。
8.构造方法可以为类中的属性初始化,构造方法与类的名称相同,无法返回值类型声明,如果在类中没有明确定义出构造方法,则会自动生成一个无参的什么都不做的构造方法,在一个类中的构造方法可以重载,但是每个类都只至少有一个构造方法。
