java代码使用重写来优化电子宠物系统

需求说明:
使用方法重写优化电子宠物系统,实现如下效果:
在这里插入图片描述
依据图片可知,我们可以建立三个类,一个是pet类,一个是dog类,还有一个penguin类,且pet类是dog类和penguin类的父类。

实现代码如下:

//Pet类

public class Pet {
private String name;//名字
private int health;//健康值
private int love;//亲密值

//show方法
public void show(){
    System.out.println("宠物的自白:\n我的名字叫:"+name+",我的健康值是:"+health+",我和主人的亲密度是"+love);
}

//宠物的构造方法
public Pet(String name, int health, int love) {
    this.name = name;
    this.health = health;
    this.love = love;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getHealth() {
    return health;
}

public void setHealth(int health) {
    this.health = health;
}

public int getLove() {
    return love;
}

public void setLove(int love) {
    this.love = love;
}

}

//Dog类

public class Dog extends Pet{
private String type;//宠物的种类

//方法的重写
@Override
public void show() {
    super.show();
    System.out.println("我是一只"+type+"犬");
}

//狗狗的构造方法
public Dog(String name, int health, int love, String type) {
    super(name, health, love);
    this.type = type;
}



public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

}

//penguin类

public class Penguin extends Pet{
private String sex;//企鹅的性别

//show方法


@Override
public void show() {
    super.show();
    System.out.println("我的性别是:"+sex);
}

//构造方法
public Penguin(String name, int health, int love, String sex) {
    super(name, health, love);
    this.sex = sex;
}

public String getSex() {
    return sex;
}

public void setSex(String sex) {
    this.sex = sex;
}

}

//测试类

public class Test1 {

public static void main(String[] args) {

    showInfo(new Dog("欧欧",100,0,"雪瑞纳"));
    showInfo(new Penguin("楠楠",100,0,"Q妹"));


}

//在方法传参时,完成向上转型
private static void showInfo(Pet pet){
    pet.show();
}

}

  • 12
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的示例代码,演示如何重写Java Spring Security的认证: 首先,创建一个类来实现UserDetailsService接口,例如: ``` @Service public class UserDetailsServiceImpl implements UserDetailsService { @Autowired private UserRepository userRepository; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { User user = userRepository.findByUsername(username); if (user == null) { throw new UsernameNotFoundException("User not found with username: " + username); } return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), new ArrayList<>()); } } ``` 在上面的代码中,我们使用了一个自定义的UserRepository来获取用户信息。如果用户不存在,则将抛出UsernameNotFoundException异常。 接下来,创建一个类来扩展WebSecurityConfigurerAdapter类: ``` @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsServiceImpl userDetailsService; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers("/", "/home").permitAll().antMatchers("/admin/**") .hasRole("ADMIN").anyRequest().authenticated().and().formLogin().loginPage("/login") .permitAll().and().logout().permitAll(); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } } ``` 在上面的代码中,我们重写了configure()方法,使用我们的UserDetailsService实现来获取用户信息,并使用BCryptPasswordEncoder来对密码进行加密。 最后,我们需要创建一个User实体类和一个UserRepository来存储用户信息。这些代码可以根据您的需求进行自定义。 ``` @Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String username; private String password; private boolean active; @ElementCollection(targetClass = Role.class, fetch = FetchType.EAGER) @CollectionTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id")) @Enumerated(EnumType.STRING) private Set<Role> roles; // getters and setters } @Repository public interface UserRepository extends JpaRepository<User, Long> { User findByUsername(String username); } ``` 上面的代码演示了如何使用Java Spring Security重写认证,并使用自定义的UserDetailsService实现来获取用户信息。根据您的需求,您可以根据自己的需要对代码进行自定义。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值