class Animal_text1 {
public static void main(String[] args) {
Cat c = new Cat();
c.eat();
c.sleep();
JumpCat jc = new JumpCat();
jc.eat();
jc.sleep();
jc.jump();
Dog d = new Dog();
d.eat();
d.sleep();
JumpCat jc1 = new JumpCat();
jc1.eat();
jc1.sleep();
jc1.jump();
}
}
abstract class Animal {
private String name;
private int age;
public Animal(){}
public Animal(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;
}
public abstract void eat();
public abstract void sleep();
}
interface Jumping {
public abstract void jump();
}
class Cat extends Animal {
public Cat(){}
public Cat(String name,int age){
super(name,age);
}
public void eat(){
System.out.println("猫吃鱼");
}
public void sleep(){
System.out.println("站着睡");
}
}
class JumpCat extends Cat implements Jumping {
public void jump(){
System.out.println("跳高猫");
}
}
class Dog extends Animal{
public Dog(){}
public Dog(String name,int age){
super(name,age);
}
public void eat (){
System.out.println("狗吃骨头");
}
public void sleep(){
System.out.println("趴着睡");
}
}
class JumpDog extends Dog implements Jumping {
public void jump(){
System.out.println("跳高狗");
}
}
老师和学生案例,加入抽烟的额外功能
class Person_Text {
public static void main(String[] args) {
Student s = new Student();
s.eat();
SmokeStudent ss = new SmokeStudent();
ss.eat();
ss.smoke();
Teacher t = new Teacher();
t.eat();
SmokeTeacher st = new SmokeTeacher();
st.eat();
st.smoke();
}
}
abstract class Person {
private String name;
private int age;
public Person(){}
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;
}
public abstract void eat();
}
interface Smoken {
public abstract void smoke();
}
class Student extends Person{
public Student(){}
public Student(String name,int age){
super(name,age);
}
public void eat(){
System.out.println("学生吃肉");
}
}
class SmokeStudent extends Student implements Smoken {
public void smoke(){
System.out.println("学生抽烟");
}
}
class Teacher extends Person{
public Teacher(){}
public Teacher(String name,int age){
super(name,age);
}
public void eat(){
System.out.println("老师喝汤");
}
}
class SmokeTeacher extends Teacher implements Smoken {
public void smoke(){
System.out.println("老师抽烟");
}
}
1.计算机对数据进行存储和运算的最小单位是:字节。
计算机能够直接识别的进制是哪一种:二进制数据。
*2 面试题:看下面的程序是否有问题,如果有问题,请指出并说明理由。
* byte b1 = 3;
* byte b2 = 4;
* byte b3 = b1 + b2;
* 从两方面去回答这个题
* b1和b2是两个变量,变量里面存储的值都是变化的,所以在程序运行中JVM是无法判断里面具体的值
* byte类型的变量在进行运算的时候,会自动类型提升为int类型
* byte b4 = 3 + 4;
* 3和4都是常量,java有常量优化机制,就是在编译的的时候直接把3和4的结果赋值给b4了
3、交换两个int类型的变量值。(三个思路都练习下)
class Lllllll {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c;
/*c = a;
a = b;
b = c;
*/
/*c = a + b;
a = c - a;
b = c - b;
*/
a = a ^ b;
b = a ^ b;
a = a ^ b;
System.out.println("a=" + a + ",b=" + b);
}
}
4 高效率运算2乘以8
2<<3 2*12 2<<3+2<<2
3、看下面程序是否有问题?
short s = 1; s = s + 1;
short s = 1; s += 1;
第一局会报错 , +=隐含了强制转换
5、* A:看程序写结果:
int x = 2;
int y = 3;
switch(x){
default:
y++;
break;
case 3:
y++;
case 4:
y++;
}
System.out.println("y="+y);
4
* B:看程序写结果:
int x = 2;
int y = 3;
switch(x){
default:
y++;
case 3:
y++;
case 4:
y++;
}
System.out.println("y="+y);
6
6、:面试题
byte可以作为switch的表达式吗? 可以
long可以作为switch的表达式吗? 不可以
String可以作为switch的表达式吗? 可以
表达式可以放什么?
byte,short,char,int,JDK1.5以后支持枚举,JDK1.7以后支持字符串(String)。
|
|